Repository navigation
[LANG-1837] Fix race conditions in lazily initialized toString caches - #1811
Open
aosen-xiong wants to merge 1 commit into
Open
aosen-xiong wants to merge 1 commit into
aosen-xiong wants to merge 1 commit into
Conversation
Fraction.toString(), Fraction.toProperString(), Range.toString() and CharRange.toString() cache their result in a non-volatile field and read that field twice: once for the null check and once for the return. If the first read sees a value written by another thread, the Java Memory Model does not order the second read after that write, so the second read may still return null and the method can return null. Read each field once into a local variable, then test and return the local. This follows up on a648173 ("Fix race condition in Fraction.hashCode()") for the remaining lazily initialized fields in these immutable classes. There is no functional change for single-threaded callers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
garydgregory
marked this pull request as draft
October 6, 2026 21:33
Member
|
@aosen-xiong |
Author
|
Hi @garydgregory, thanks for your reply. You are right, there is no regression test in this PR. However, for this kind of concurrency issue, I am not aware of any stable regression test we can add to trigger this failure in CI before the fix and pass after the fix. The OpenJDK PRs showed the same pattern. See openjdk/jdk#9608 and https://github.com/openjdk/jdk/pull/6870/changes. |
aosen-xiong
marked this pull request as ready for review
October 9, 2026 03:14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Jira: LANG-1837
Fraction.toString(),Fraction.toProperString(),Range.toString()andCharRange.toString()cache their result in a non-volatile field and read that field twice: once for the null check and once for the return.If the first read sees a value written by another thread, the Java Memory Model does not order the second read after that write, so the second read may still return
null(JLS 17.4) and the method can returnnull. All three classes are documented as immutable.This change reads each field once into a local variable, then tests and returns the local. There is no functional change for single-threaded callers.
Precedent
Testing
The existing
FractionTest,RangeTestandCharRangeTestcover the string formats and pass. I did not add a test that fails without the change: the stale read is permitted by the memory model but cannot be triggered on demand.How this was found
By a static analysis for this pattern, confirmed by reading the code. I have not observed a failure.
Thanks for your contribution to Apache Commons! Your help is appreciated!
Before you push a pull request, review this list:
Co-Authored-Byline.mvn; that'smvnon the command line by itself. BUILD SUCCESS on JDK 21: 85,511 tests, 0 failures, 0 Checkstyle violations.