Skip to content
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

8323670: A few client tests intermittently throw ConcurrentModificationException #17462

Closed
wants to merge 5 commits into from

Conversation

TejeshR13
Copy link
Contributor

@TejeshR13 TejeshR13 commented Jan 17, 2024

Suggested fix JDK-8307091 also created concurrent exception intermittently (monthly once/quarterly once) on CI system. The issue was not able to be reproduced yet, hence proposing an alternative fix which uses iterators to compare the List.
CI testing shows green.


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-8323670: A few client tests intermittently throw ConcurrentModificationException (Bug - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 17462

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 17, 2024

👋 Welcome back tr! 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 Jan 17, 2024

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

  • client

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.

@openjdk openjdk bot added the client client-libs-dev@openjdk.org label Jan 17, 2024
@TejeshR13 TejeshR13 changed the title Fix 8323670: A few client tests intermittently throw ConcurrentModificationException Jan 17, 2024
@openjdk openjdk bot added the rfr Pull request is ready for review label Jan 17, 2024
@mlbridge
Copy link

mlbridge bot commented Jan 17, 2024

Webrevs

@mrserb
Copy link
Member

mrserb commented Jan 19, 2024

What is the code path which modifies the vector when we iterate it?

@TejeshR13
Copy link
Contributor Author

What is the code path which modifies the vector when we iterate it?

I don't think we are able to trace it out, since the issue intermittent and previously I had made a copy of the vector list before checking for equality of the list. There was again an issue in the code which I used to copy to a temporary vector. So now instead of using AbstractList.equals I'm using iterators and comparing every element within synchronized method.

@@ -412,6 +413,18 @@ private void cancelRunnables() {
runnable.cancel();
}
}

private synchronized <T> boolean compareIterators(Iterator<T> iterator1, Iterator<T> iterator2) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I understand, how this synchronized helps to avoid the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since concurrent modification exception is thrown, it is clear that the List is being modified while comparing two list. Hence instead of copying the list locally, I have used iterators and comparing element by element in a synchronized method which ensures single thread is accessing the iterators. Without synchronized I guess it would again cause concurrentModificationException.

Copy link
Member

Choose a reason for hiding this comment

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

Vector.iterator and Vector.subList.iterator are still check for modification on iteration (see usages of the method java.util.AbstractList.SubList#checkForComodification). It means, if vector was concurrently modified during iteration - iteration will fail with the ConcurrentModificationException

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, which is why I am using Synchronized to handle concurrentModificationException.

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't work like this. Modification happen in another thread in another method. This synchronized doesn't affect another method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using synchronized (fileCache) inside ShellFolder.invoke would be better and one solution right? Than making local copy and again doing sanity checks/changing to ArrayList?

Copy link
Member

Choose a reason for hiding this comment

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

Using synchronized (fileCache) inside ShellFolder.invoke would be better and one solution right? Than making local copy and again doing sanity checks/changing to ArrayList?

I'm afraid I can't answer this question without more details on what is achieved by this code. We need to look closely into what was done for JDK-8240690: Race condition between EDT and BasicDirectoryModel.FilesLoader.run0().

Putting the entire method into synchronized (fileCache) is an easy solution. Yet any other thread which accesses fileCache will be blocked until the code exits the synchronized block in the call method. This somewhat defeats updating the file list in the background, doesn't it?

Even if you'll go this route, I'm for replacing Vector with ArrayList for the newFileCache and newFiles variables. These are local variables, they're not accessed concurrently. Yet they're accessed from two threads: the current one and the one where ShellFolder.invoke runs, so there could be a need to use another synchronisation technique to ensure thread-safety between these two threads.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even if you'll go this route, I'm for replacing Vector with ArrayList for the newFileCache and newFiles variables. These are local variables, they're not accessed concurrently. Yet they're accessed from two threads: the current one and the one where ShellFolder.invoke runs, so there could be a need to use another synchronisation technique to ensure thread-safety between these two threads.

Another Synchronization technique even after synchronized (fileCache)?

Copy link
Member

Choose a reason for hiding this comment

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

Even if you'll go this route, I'm for replacing Vector with ArrayList for the newFileCache and newFiles variables. These are local variables, they're not accessed concurrently. Yet they're accessed from two threads: the current one and the one where ShellFolder.invoke runs, so there could be a need to use another synchronisation technique to ensure thread-safety between these two threads.

Another Synchronization technique even after synchronized (fileCache)?

Yes, even after.

Copy link
Member

@aivanov-jdk aivanov-jdk Jan 30, 2024

Choose a reason for hiding this comment

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

…I'm for replacing Vector with ArrayList for the newFileCache and newFiles variables. These are local variables, they're not accessed concurrently. Yet they're accessed from two threads: the current one and the one where ShellFolder.invoke runs, so there could be a need to use another synchronisation technique to ensure thread-safety between these two threads.

Another Synchronization technique even after synchronized (fileCache)?

Yes, even after.

No, it's not needed, actually. ShellFolder.invoke executes the code directly on the same thread or passes it to COM thread on Windows. When it's passed to the COM thread, a Future object is used, which ensures synchronisation.

I submitted JDK-8324973: Replace Vector with ArrayList in BasicDirectoryModel.FilesLoader.

@aivanov-jdk
Copy link
Member

In JDK-8307091, ConcurrentModificationException is thrown from equals, likely from this line:

&& newFileCache.subList(end, newSize).equals(fileCache.subList(start, oldSize))) {

Here, fileCache is passed as a parameter, it can be modified while newFileCache.subList().equals() runs. You could've put this into synchronized(fileCache) to solve the problem.

In JDK-8323670, the exception is thrown from Vector constructor:

List<File> listStart_OldSize = new Vector<>(fileCache.subList(start, oldSize));

Here, we have exactly the same problem: fileCache, well fileCache.subList to be exact, is iterated without locking. (The subList uses the same object as the lock object, which is fileCache.) If you move this line into synchronized(fileCache), the problem would resolved.


However, as I pointed out above, the code in the call method may still fail. There are at least two bugs where ConcurrentModificationException is thrown, which means fileCache can be modified. The for-loops access elements in fileCache using the variable oldSize as the size of the collection. Since the size can grow or shrink, it is possible that the code requests an element which is not in the fileCache any more.

Yes, putting everything into synchronized(fileCache) will resolve this problem too. Is it the best solution?

@TejeshR13
Copy link
Contributor Author

Instead of putting everything into synchronized(fileCache), I guess the better solution would be to wrap this line

List<File> listStart_OldSize = new Vector<>(fileCache.subList(start, oldSize));

into synchronized(fileCache). With this we can ensure that a local copy would be made before comparing two fileCache list, since we cannot/shouldn't depend on any addition/deletion from fileCache.

@aivanov-jdk
Copy link
Member

aivanov-jdk commented Jan 25, 2024

Instead of putting everything into synchronized(fileCache), I guess the better solution would be to wrap this line

List<File> listStart_OldSize = new Vector<>(fileCache.subList(start, oldSize));

into synchronized(fileCache). With this we can ensure that a local copy would be made before comparing two fileCache list, since we cannot/shouldn't depend on any addition/deletion from fileCache.

But this won't resolve the problem completely.

A better solution could still be creating a local copy of fileCache and then use it to calculating the diffs.

There's still a problem: if fileCache is mutated again, the diffs become outdated. It's not taken into account by the current code. We can still ignore it.

Then I haven't figured it out for what the calculated diffs are used. I also see that the evaluation can be cancelled.

In fact, I see more thread-safety issues in the implementation… There are some fields and variables which are accessed without proper synchronisation.

I looked at the code once again after reading @mrserb's code review for JDK-8240690, it seems that it goes like this:

  1. The loader thread is created. Its run0 method populates the files from file system. (It does so using fileSystem.getFiles which may be not thread-safe as well.)
  2. The list of files is filtered (which also uses possibly non-thread-safe methods from JFileChooser).
  3. The list of files is sorted.
  4. The code inside ShellFolder.invoke is run on yet another thread. It calculates the diffs between the current fileCache and newFileCache created up to step 3.
  5. The result of step 4 is an object of DoChangeContents which contains a snapshot of added or removed files. The object implements Runnable interface and is assigned to the field runnable.
  6. This runnable is posted to be executed on EDT via invokeLater. It updates fileCache.

Step 6 uses doFire variable, it could be set to false using the cancel method, which is synchronized. As such, reading the value of doFire must also be synchronised.

Looking at the methods of BasicDirectoryModel, I can see that some methods use synchronized(fileCache) before accessing the field but others (getSize, contains, indexOf, getElementAt), which makes the BasicDirectoryModel class not thread-safe; it is especially dangerous with contains and indexOf where the contents of the fileCache can be changed while the method is iterating over the values inside fileCache.

@aivanov-jdk
Copy link
Member

Taking the above into account, I appears that the code inside ShellFolder.invoke.call should actually be inside synchronized (fileCache). It would resolve ConcurrentModificationException.

And you should revert the changes you made in JDK-8323670. This bug is the proof that fix didn't help.

Ideally, we should have a test which reproduces the problem… Yet, as with all concurrency issues, writing such a test could be very hard.

@TejeshR13
Copy link
Contributor Author

Taking the above into account, I appears that the code inside ShellFolder.invoke.call should actually be inside synchronized (fileCache). It would resolve ConcurrentModificationException.

And you should revert the changes you made in JDK-8323670. This bug is the proof that fix didn't help.

Ideally, we should have a test which reproduces the problem… Yet, as with all concurrency issues, writing such a test could be very hard.

Yeah, sure then I'll revert the changes and update the changes.

@aivanov-jdk
Copy link
Member

aivanov-jdk commented Jan 25, 2024

Looking at the methods of BasicDirectoryModel, I can see that some methods use synchronized(fileCache) before accessing the field but others (getSize, contains, indexOf, getElementAt), which makes the BasicDirectoryModel class not thread-safe; it is especially dangerous with contains and indexOf where the contents of the fileCache can be changed while the method is iterating over the values inside fileCache.

I've just submitted JDK-8324719: Missing synchronized block in BasicDirectoryModel to address this synchronisation issue.

It was my mistake: Vector is a synchronised collection, therefore its methods are synchronised. Other methods need an explicit synchronised block because they manipulate the data in fileCache before returning the result.

@aivanov-jdk
Copy link
Member

Step 6 uses doFire variable, it could be set to false using the cancel method, which is synchronized. As such, reading the value of doFire must also be synchronised.

It's not a problem, the run method is synchronized.

There's a bug against it: JDK-8238169: BasicDirectoryModel getDirectories and DoChangeContents.run can deadlock.

Does run need to be synchronised? Probably not, only reading the value of doFire needs to be.

@mrserb
Copy link
Member

mrserb commented Jan 26, 2024

I don't think we are able to trace it out, since the issue intermittent and previously I had made a copy of the vector list before checking for equality of the list. There was again an issue in the code which I used to copy to a temporary vector. So now instead of using AbstractList.equals I'm using iterators and comparing every element within synchronized method.

I think we should start investigating this one, probably by adding special delays/asserts into the JDK to track down on what threads the data is modified and used.

@aivanov-jdk
Copy link
Member

I don't think we are able to trace it out, since the issue intermittent and previously I had made a copy of the vector list before checking for equality of the list. There was again an issue in the code which I used to copy to a temporary vector. So now instead of using AbstractList.equals I'm using iterators and comparing every element within synchronized method.

I think we should start investigating this one, probably by adding special delays/asserts into the JDK to track down on what threads the data is modified and used.

According to my analysis above, there there are two threads. (The third thread is also possible if validateFileCache is called not on EDT.) The only thread that modifies fileCache is EDT with DoChangeContents. Then the ShellFolder thread reads from fileCache without synchronisation, which allows the contents of fileCache to be modified concurrently.

@aivanov-jdk
Copy link
Member

I don't think we are able to trace it out, since the issue intermittent and previously I had made a copy of the vector list before checking for equality of the list. There was again an issue in the code which I used to copy to a temporary vector. So now instead of using AbstractList.equals I'm using iterators and comparing every element within synchronized method.

I think we should start investigating this one, probably by adding special delays/asserts into the JDK to track down on what threads the data is modified and used.

According to my analysis above, there there are two threads. (The third thread is also possible if validateFileCache is called not on EDT.) The only thread that modifies fileCache is EDT with DoChangeContents. Then the ShellFolder thread reads from fileCache without synchronisation, which allows the contents of fileCache to be modified concurrently.

Upon further testing, I found out that ShellFolder.invoke runs in the calling thread on Linux and macOS, on Windows it runs on the COM thread where Windows Shell is accessed.

In addition to this, there's AquaFileSystemModel. This class is used in Aqua Look-and-Feel on macOS.

According to this comment

* Some of it came from BasicDirectoryModel

the class contains portions of code copied from BasicDirectoryModel and therefore could have a similar synchronisation issue.

@aivanov-jdk
Copy link
Member

@mrserb I've been trying to write a regression test for this problem. Have I succeeded? Not quite… I am unable to get ConcurrentModificationException on any platform when I run the test locally. However, the test fails in the Oracle CI on macOS and Linux, both x86_64 and aarch64, especially when run with JTREG=REPEAT_COUNT=50. On Windows, it fails with OutOfMemoryError.

In the CI on macOS, the test runs in headless mode, in this case BasicDirectoryModel is used instead of AquaFileSystemModel.

Thus, the test is not stable enough. Yet it still allows testing the fix. The test does not fail with ConcurrentModificationException when run on the build with the proposed fix in this PR.

Could you please run the test too?

git fetch https://github.com/aivanov-jdk/jdk.git 8323670-BasicDirectoryModel-concurrency:8323670-BasicDirectoryModel-concurrency
git checkout 8323670-BasicDirectoryModel-concurrency
java test/jdk/javax/swing/JFileChooser/FileSystemView/BasicDirectoryModelConcurrency.java

The commands above are similar to those provided in “Reviewing using Git” section in PRs on GitHub.

Copy link
Member

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

I cannot get ConcurrentModificationException when running my test, BasicDirectoryModelConcurrency.java, on a build with the fix.

@openjdk
Copy link

openjdk bot commented Jan 30, 2024

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

8323670: A few client tests intermittently throw ConcurrentModificationException

Reviewed-by: aivanov, serb

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 28 new commits pushed to the master branch:

  • 68206b5: 8324585: JVM native memory leak in PCKS11-NSS security provider
  • 1aba78f: 8324937: GHA: Avoid multiple test suites per job
  • a663248: 8324668: JDWP process management needs more efficient file descriptor handling
  • a2229b1: 8324838: test_nmt_locationprinting.cpp broken in the gcc windows build
  • 432756b: 8325024: java/security/cert/CertPathValidator/OCSP/OCSPTimeout.java incorrect comment information
  • fe78c0f: 8325022: Incorrect error message on client authentication
  • 5b9b176: 8324174: assert(m->is_entered(current)) failed: invariant
  • 0cc8e5b: 8325042: remove unused JVMDITools test files
  • f292053: 8323621: JDK build should exclude snippet class in java.lang.foreign
  • 62c9530: 8324238: [macOS] java/awt/Frame/ShapeNotSetSometimes/ShapeNotSetSometimes.java fails with the shape has not been applied msg
  • ... and 18 more: https://git.openjdk.org/jdk/compare/fd8adf308357355bd33916ad80e2328c35434e5a...master

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.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 30, 2024
@aivanov-jdk
Copy link
Member

I forgot to add a direct link to the test: BasicDirectoryModelConcurrency.java

@mrserb I've been trying to write a regression test for this problem. Have I succeeded? Not quite… I am unable to get ConcurrentModificationException on any platform when I run the test locally.

I reproduced the issue a few times on macOS and once on Linux, on a local host rather than in CI.

@TejeshR13
Copy link
Contributor Author

I forgot to add a direct link to the test: BasicDirectoryModelConcurrency.java

@mrserb I've been trying to write a regression test for this problem. Have I succeeded? Not quite… I am unable to get ConcurrentModificationException on any platform when I run the test locally.

I reproduced the issue a few times on macOS and once on Linux, on a local host rather than in CI.

Not able to reproduce in CI?

@aivanov-jdk
Copy link
Member

I forgot to add a direct link to the test: BasicDirectoryModelConcurrency.java

@mrserb I've been trying to write a regression test for this problem. Have I succeeded? Not quite… I am unable to get ConcurrentModificationException on any platform when I run the test locally.

I reproduced the issue a few times on macOS and once on Linux, on a local host rather than in CI.

Not able to reproduce in CI?

The opposite. It fails in Oracle CI, yet it's still not stable enough.

And the test doesn't clean up the files when it fails. So it needs additional work.

@TejeshR13
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Feb 1, 2024

Going to push as commit 70e7cdc.
Since your change was applied there have been 34 commits pushed to the master branch:

  • ac1cd31: 8325096: Test java/security/cert/CertPathBuilder/akiExt/AKISerialNumber.java is failing
  • 8e45182: 8324834: Use _LARGE_FILES on AIX
  • cab74b0: 8324287: Record total and free swap space in JFR
  • 6b84f9b: 8325001: Typo in the javadocs for the Arena::ofShared method
  • cd11059: 8325053: Serial: Move Generation::save_used_region to TenuredGeneration
  • d9331bf: 8324845: management.properties text "interface name" is misleading
  • 68206b5: 8324585: JVM native memory leak in PCKS11-NSS security provider
  • 1aba78f: 8324937: GHA: Avoid multiple test suites per job
  • a663248: 8324668: JDWP process management needs more efficient file descriptor handling
  • a2229b1: 8324838: test_nmt_locationprinting.cpp broken in the gcc windows build
  • ... and 24 more: https://git.openjdk.org/jdk/compare/fd8adf308357355bd33916ad80e2328c35434e5a...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Feb 1, 2024
@openjdk openjdk bot closed this Feb 1, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Feb 1, 2024
@openjdk
Copy link

openjdk bot commented Feb 1, 2024

@TejeshR13 Pushed as commit 70e7cdc.

💡 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
client client-libs-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

4 participants