[hotfix] make previousCpuTicks and previousProcCpuTicks volatile to increase visibility for all threads#26257
Conversation
|
|
||
| private long[] previousCpuTicks; | ||
| private long[][] previousProcCpuTicks; | ||
| private volatile long[] previousCpuTicks; |
There was a problem hiding this comment.
I had a look at the usages of these 2 fields
it seems previousCpuTicks is only updated if it is null. Which looks very strange - is this right? I can see that volatile could be useful here if this is correct processing.
previousProcCpuTicks is updated when null then after processing in the method see code here. I wonder if the
previousCpuTicks logic should update it, in the same way as previousProcCpuTicks .
I was looking at these usages to see if AtomicReference should be used rather than volatile. This would be more thread safe, and to use compareAndSet() to update it.
There was a problem hiding this comment.
It seems to me that it is also the only place it can be initialized.
I agree with AtomicRefrence, I was thinking about the same thing. Should I add an commit with that? :)
|
This PR is being marked as stale since it has not had any activity in the last 90 days. If you are having difficulty finding a reviewer, please reach out to the If this PR is no longer valid or desired, please feel free to close it. |
|
This PR is being marked as stale since it has not had any activity in the last 90 days. If you are having difficulty finding a reviewer, please reach out to the If this PR is no longer valid or desired, please feel free to close it. |
What is the purpose of the change
This pull request fixes a visibility issue in the
SystemResourcesCounterclass by markingpreviousCpuTicksandpreviousProcCpuTicksas volatile.Previously, these variables were not declared as volatile, meaning updates made by one thread might not be immediately visible to others. This could lead to stale values being read by different threads.
By making these variables volatile, we ensure that all threads see the most up-to-date values, preventing inconsistencies.
Brief change log
previousCpuTicksandpreviousProcCpuTicksas volatile to ensure proper visibility across threads.Verifying this change
This change is a trivial rework/code cleanup without any test coverage.
Does this pull request potentially affect one of the following parts?
@Public(Evolving))? NoDocumentation