Repository navigation
Conversation
Replace synchronized blocks with ReentrantReadWriteLock to ensure that external code cannot participate in the NestedJarFile instance's own concurrency handling. The previous synchronized approach could lead to external code synchronizing on the same monitor which can result in a deadlock. Closes spring-projectsgh-51463 Closes spring-projectsgh-51379 Signed-off-by: Ian Kettle <[email protected]>
Member
This isn't really our choice. Thanks anyway for the PR. |
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.
Replace synchronized blocks with ReentrantReadWriteLock to ensure that external code cannot participate in the NestedJarFile instance's own concurrency handling. The previous synchronized approach could lead to external code synchronizing on the same monitor which can result in a deadlock.
Reproduction
NestedJarFileLockOrderingDeadlockTests(added in this PR) reproduces the deadlock deterministically using plain threads - twoCountDownLatches force one thread to hold an external lock while waiting onNestedJarFile's monitor, and another to hold that monitor while waiting on the external lock. As shipped, the test uses a 5 second timeout so it fails fast in CI rather than hanging.Removing that timeout (
bothThreadsFinished.await(5, TimeUnit.SECONDS)->bothThreadsFinished.await()) and running it against the currentsynchronized (this)implementation hangs the JVM indefinitely. Ajcmd <pid> Thread.printagainst the hung process confirms the exact deadlock:This is the same shape as the thread dumps in gh-51463 and gh-51379 -
NestedJarFile.hasEntry()blocked waiting to lock aNestedJarFile/UrlNestedJarFileinstance, with a second thread holding that same lock while blocked on something else. gh-51379 was closed for lack of a minimal reproduction; this test and dump are that reproduction.This also isn't specific to virtual threads - the reproduction above uses plain platform threads. The underlying defect is
NestedJarFilelocking onsynchronized (this), exposing its own monitor to any external code that happens to synchronize on the same instance. Virtual threads make it easier to hit in practice (no thread-pool queueing to throttle concurrent classloading traffic), but they're not the cause.Closes gh-51463
Closes gh-51379