Make readOnlyBearerAuth volatile to prevent data races when executing… #4375
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have created a related issue here: #4374
Description of the issue and possible solution:
There is a data race (according to the Java memory model) in the field
readOnlyBearerAuth
in classRegistryClient.java
. Due to this data race, it is possible that writes on the variable by one thread are not visible to other threads reading the variable in the future. This can happen, for instance, if two threads concurrent executedoPullBearerAuth()
anddoPushBearerAuth()
. Here is an example of a thread reading stale or outdated values ofreadOnlyBearerAuth
:Consider two threads t1 and t2 and let
readOnlyBearerAuth==false
. Consider that t1 executesdoPullBearerAuth()
and concurrently t2 executesdoPushBearerAuth()
. The following execution can occur:readOnlyBearerAuth
as false in this linereadOnlyBearerAuth
as false (because there is no happens-before relation between the write by t1 and this read and the java memory model does not ensure that the write by t1 is visible to t2)Solution to the problem in this PR
Declaring
readOnlyBearerAuth
as volatile. This removes the data race, and it ensures (as per the java memory model) that the reads always read the latest write onreadOnlyBearerAuth
Priority
The execution above may not always occur, but, when it does, it will set incorrectly
authorization
in the classRegistryClient.java
.Fixes #4374 🛠️