8354522: Clones of DecimalFormat cause interferences when used concurrently#24598
8354522: Clones of DecimalFormat cause interferences when used concurrently#24598j3graham wants to merge 10 commits into
Conversation
|
👋 Welcome back j3graham! A progress list of the required criteria for merging this PR into |
|
@j3graham This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be: You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 86 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@justin-curtis-lu, @naotoj) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
|
Reproducing code example: public class DecimalFormatTest {
static AtomicInteger mismatchCount = new AtomicInteger(0);
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("#");
String str = df.format(Math.PI); // initial use of formatter
System.out.println(str);
try (var ex = Executors.newThreadPerTaskExecutor(Thread.ofPlatform().factory())) {
for (int i = 0; i < 50; i++) {
// each thread gets its own clone of df
DecimalFormat threadDf = (DecimalFormat) df.clone();
ex.execute(makeTask(threadDf));
}
}
System.out.println("mismatchCount = " + mismatchCount);
}
private static Runnable makeTask(DecimalFormat threadDf) {
return () -> {
for (int i = 0; i < 1000000; i++) {
String dfString = threadDf.format(BigDecimal.valueOf(i));
String str = String.valueOf(i);
if (!str.equals(dfString)) {
System.err.println("mismatch: str = " + str + " dfString = " + dfString);
mismatchCount.incrementAndGet();
}
}
};
}
} |
Webrevs
|
|
Are you going to turn the reproducer into a test? I don't know if we have tests that for DecimalFormat cloning. |
|
I didn't notice any existing tests - I've added a version of the reproducer. |
justin-curtis-lu
left a comment
There was a problem hiding this comment.
Thanks for the fix, it looks good. Left some minor comments. I will run tiers 1-3 with your change.
| char[] newDigits = new char[digits.length]; | ||
| System.arraycopy(digits, 0, newDigits, 0, digits.length); | ||
| other.digits = newDigits; | ||
| other.data = null; |
There was a problem hiding this comment.
We don't have to copy the data array unlike the digits array (above) because it can just be reset during the next getDataChars call, but a comment as to why might be helpful.
| // each thread gets its own clone of df | ||
| DecimalFormat threadDf = (DecimalFormat) df.clone(); | ||
| Runnable task = () -> { | ||
| for (int j = 0; j < 1000000; j++) { |
There was a problem hiding this comment.
We should probably make this test less costly by decreasing some values, I don't the bug requires that many iterations to be exposed. (Without the fix and the break statement in the test, mismatchCount gets up into the tens of thousands on my machine.)
There was a problem hiding this comment.
For the original reproducer I wanted to be really sure that it failed. I've reduced it to be more reasonable. With the current config it still gets 10-100 mismatches for me. I've limited the number of lines that get logged as well.
There was a problem hiding this comment.
If a mismatch is found, I think we can stop there and report error
There was a problem hiding this comment.
My thinking was that for a test pass, it would never hit the shortcut anyway, so it’s only an optimization for the failure case, which hopefully won’t happen anymore. Seeing the count of all collisions gives some reassurance that the test is still running for long enough to be effective.
There was a problem hiding this comment.
This is a regression test. Detecting the regression and keeping doing the same test is unnecessary.
There was a problem hiding this comment.
I’ve updated the test to exit early.
| /* | ||
| * @test | ||
| * @bug 8354522 | ||
| * @summary Check for cloning interference |
There was a problem hiding this comment.
It will probably be good to mention somewhere that this test/fix addresses the issue of the same data array reference being shared amongst DigitList clones.
There was a problem hiding this comment.
I added more detail to the comment with the test method.
| public void testCloneIndependence() { | ||
| AtomicInteger mismatchCount = new AtomicInteger(0); | ||
| DecimalFormat df = new DecimalFormat("#"); | ||
| String _ = df.format(Math.PI); // initial use of formatter |
There was a problem hiding this comment.
We should probably comment the importance of this line, as without it the test will pass without the fix. (It sets the data array to a non null value).
| } | ||
| String str = String.valueOf(value); | ||
| if (!str.equals(dfString)) { | ||
| mismatchCount.getAndIncrement(); |
There was a problem hiding this comment.
mismatchCount is no longer needed. Simply break after printing the error message would suffice.
There was a problem hiding this comment.
It would still need something to indicate that it had failed, as well as a way to signal other threads that they should terminate early. Worth changing to an AtomicBoolean?
There was a problem hiding this comment.
Ok to leave as is? Or is the AtomicBoolean more clear?
There was a problem hiding this comment.
Ok to use it as a failure indicator (still I'd suggest breaking immediately after printing error)
| System.err.println("mismatch: str = " + str + " dfString = " + dfString); | ||
| } | ||
| } | ||
| } catch (InterruptedException e) { |
There was a problem hiding this comment.
I'd suggest throwing the exception (or RuntimeException with it as the cause), not swallowing it silently in the test.
|
I just wonder if we could do a white testing, ie, |
|
I've added a reflection-based test to see how it looks. We could go with either test, or both. |
naotoj
left a comment
There was a problem hiding this comment.
Thanks for adding the test. Let's keep them both.
| static void setup() throws Exception { | ||
| DIGIT_LIST_FIELD = DecimalFormat.class.getDeclaredField("digitList"); | ||
| DIGIT_LIST_FIELD.setAccessible(true); | ||
|
|
||
| DecimalFormat df = new DecimalFormat(); | ||
| Object digitList = DIGIT_LIST_FIELD.get(df); | ||
|
|
||
| DIGIT_LIST_CLASS = digitList.getClass(); | ||
| } |
There was a problem hiding this comment.
I'd add some comment saying that this is a whitebox testing, so it might fail if the internal implementation changes (quite unlikely though). And if accessing digitList/data fails, it should throw jtreg.SkippedException instead of a real exception in the actual @test methods.
justin-curtis-lu
left a comment
There was a problem hiding this comment.
Thanks for the changes, the new test looks good to me.
naotoj
left a comment
There was a problem hiding this comment.
Looks good to me. Thanks for fixing this.
|
Thank you all for the reviews. |
|
/integrate |
|
/sponsor |
|
Going to push as commit 04c32fc.
Your commit was automatically rebased without conflicts. |
The
DigitListclass used inDecimalFormatdoes not reset thedataarray in its clone method. This can cause interference when clones are used concurrently.Progress
Issue
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/24598/head:pull/24598$ git checkout pull/24598Update a local copy of the PR:
$ git checkout pull/24598$ git pull https://git.openjdk.org/jdk.git pull/24598/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 24598View PR using the GUI difftool:
$ git pr show -t 24598Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/24598.diff
Using Webrev
Link to Webrev Comment