Skip to content

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

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

Closed
wants to merge 10 commits into from

Conversation

j3graham
Copy link
Contributor

@j3graham j3graham commented Apr 11, 2025

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

Sorry, something went wrong.

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 11, 2025

👋 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
Copy link

openjdk bot commented Apr 11, 2025

@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
Copy link

openjdk bot commented Apr 11, 2025

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

@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
Copy link

mlbridge bot commented Apr 15, 2025

@AlanBateman
Copy link
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
Contributor Author

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

Copy link
Member

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

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.

@@ -725,6 +725,7 @@ public Object clone() {
char[] newDigits = new char[digits.length];
System.arraycopy(digits, 0, newDigits, 0, digits.length);
other.digits = newDigits;
other.data = null;
Copy link
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
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
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
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
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
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
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
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
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
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
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
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.

Copy link
Contributor Author

@j3graham j3graham Apr 15, 2025

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

Copy link
Contributor Author

@j3graham j3graham Apr 15, 2025

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?

Copy link
Member

@naotoj naotoj Apr 15, 2025

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)

String str = String.valueOf(value);
if (!str.equals(dfString)) {
mismatchCount.getAndIncrement();
System.err.println("mismatch: str = " + str + " dfString = " + dfString);
}
}
} catch (InterruptedException e) {
Copy link
Member

@naotoj naotoj Apr 15, 2025

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
Copy link
Member

naotoj commented Apr 15, 2025

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
Contributor Author

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

Copy link
Member

@naotoj naotoj left a comment

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

Copy link
Member

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

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.

Copy link
Member

@naotoj naotoj left a comment

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
Copy link
Contributor Author

j3graham commented Apr 17, 2025

Thank you all for the reviews.

@j3graham
Copy link
Contributor Author

/integrate

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

openjdk bot commented Apr 17, 2025

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

@naotoj
Copy link
Member

naotoj commented Apr 17, 2025

/sponsor

@openjdk
Copy link

openjdk bot commented Apr 17, 2025

Going to push as commit 04c32fc08a67eaf6d3f47a0f9ea3d620b7ec6a07.
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
Copy link

openjdk bot commented Apr 17, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs [email protected] integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

None yet

4 participants