-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[hotfix] make previousCpuTicks and previousProcCpuTicks volatile to increase visibility for all threads #26257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[hotfix] make previousCpuTicks and previousProcCpuTicks volatile to increase visibility for all threads #26257
Conversation
@@ -53,8 +53,8 @@ public class SystemResourcesCounter extends Thread { | |||
|
|||
private volatile boolean running = true; | |||
|
|||
private long[] previousCpuTicks; | |||
private long[][] previousProcCpuTicks; | |||
private volatile long[] previousCpuTicks; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comments
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
SystemResourcesCounter
class by markingpreviousCpuTicks
andpreviousProcCpuTicks
as 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
previousCpuTicks
andpreviousProcCpuTicks
as 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