Skip to content

8354522: Clones of DecimalFormat cause interferences when used concurrently#24598

Closed
j3graham wants to merge 10 commits into
openjdk:masterfrom
j3graham:digitlist-clone
Closed

8354522: Clones of DecimalFormat cause interferences when used concurrently#24598
j3graham wants to merge 10 commits into
openjdk:masterfrom
j3graham:digitlist-clone

Conversation

@j3graham

@j3graham j3graham commented Apr 11, 2025

Copy link
Copy Markdown
Contributor

The DigitList class used in DecimalFormat does not reset the data array in its clone method. This can cause interference when clones are used concurrently.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8354522: Clones of DecimalFormat cause interferences when used concurrently (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/24598/head:pull/24598
$ git checkout pull/24598

Update a local copy of the PR:
$ git checkout pull/24598
$ git pull https://git.openjdk.org/jdk.git pull/24598/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24598

View PR using the GUI difftool:
$ git pr show -t 24598

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/24598.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper

bridgekeeper Bot commented Apr 11, 2025

Copy link
Copy Markdown

👋 Welcome back j3graham! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk

openjdk Bot commented Apr 11, 2025

Copy link
Copy Markdown

@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:

8354522: Clones of DecimalFormat cause interferences when used concurrently

Reviewed-by: jlu, naoto

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 master branch:

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 /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk

openjdk Bot commented Apr 11, 2025

Copy link
Copy Markdown

@j3graham The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@j3graham

Copy link
Copy Markdown
Contributor Author

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();
                }
            }
        };
    }
}

@openjdk openjdk Bot added the core-libs core-libs-dev@openjdk.org label Apr 11, 2025
@j3graham j3graham changed the title Clones of DecimalFormat can share state 8354522: Clones of DecimalFormat can share state Apr 15, 2025
@j3graham j3graham changed the title 8354522: Clones of DecimalFormat can share state 8354522: Clones of DecimalFormat cause interferences when used concurrently Apr 15, 2025
@openjdk openjdk Bot added the rfr Pull request is ready for review label Apr 15, 2025
@mlbridge

mlbridge Bot commented Apr 15, 2025

Copy link
Copy Markdown

@AlanBateman

Copy link
Copy Markdown
Contributor

Are you going to turn the reproducer into a test? I don't know if we have tests that for DecimalFormat cloning.

@j3graham

Copy link
Copy Markdown
Contributor Author

I didn't notice any existing tests - I've added a version of the reproducer.

@justin-curtis-lu justin-curtis-lu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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++) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a mismatch is found, I think we can stop there and report error

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a regression test. Detecting the regression and keeping doing the same test is unnecessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve updated the test to exit early.

/*
* @test
* @bug 8354522
* @summary Check for cloning interference

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
String str = String.valueOf(value);
if (!str.equals(dfString)) {
mismatchCount.getAndIncrement();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mismatchCount is no longer needed. Simply break after printing the error message would suffice.

@j3graham j3graham Apr 15, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok then. Thanks.

@j3graham j3graham Apr 15, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to leave as is? Or is the AtomicBoolean more clear?

@naotoj naotoj Apr 15, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

@naotoj naotoj Apr 15, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest throwing the exception (or RuntimeException with it as the cause), not swallowing it silently in the test.

@naotoj

naotoj commented Apr 15, 2025

Copy link
Copy Markdown
Member

I just wonder if we could do a white testing, ie, df.digitList.data != df.clone().digitList.data after df.format(Math.PI)

@j3graham

Copy link
Copy Markdown
Contributor Author

I've added a reflection-based test to see how it looks. We could go with either test, or both.

@naotoj naotoj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test. Let's keep them both.

Comment on lines +48 to +56
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();
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 justin-curtis-lu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes, the new test looks good to me.

@naotoj naotoj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks for fixing this.

@openjdk openjdk Bot added the ready Pull request is ready to be integrated label Apr 16, 2025
@j3graham

j3graham commented Apr 17, 2025

Copy link
Copy Markdown
Contributor Author

Thank you all for the reviews.

@j3graham

Copy link
Copy Markdown
Contributor Author

/integrate

@openjdk openjdk Bot added the sponsor Pull request is ready to be sponsored label Apr 17, 2025
@openjdk

openjdk Bot commented Apr 17, 2025

Copy link
Copy Markdown

@j3graham
Your change (at version 6c23e27) is now ready to be sponsored by a Committer.

@naotoj

naotoj commented Apr 17, 2025

Copy link
Copy Markdown
Member

/sponsor

@openjdk

openjdk Bot commented Apr 17, 2025

Copy link
Copy Markdown

Going to push as commit 04c32fc.
Since your change was applied there have been 93 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk Bot added the integrated Pull request has been integrated label Apr 17, 2025
@openjdk openjdk Bot closed this Apr 17, 2025
@openjdk openjdk Bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Apr 17, 2025
@openjdk

openjdk Bot commented Apr 17, 2025

Copy link
Copy Markdown

@naotoj @j3graham Pushed as commit 04c32fc.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@j3graham j3graham deleted the digitlist-clone branch May 16, 2025 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

4 participants