Skip to content

Fix deadlock participation in NestedJarFile - #51552

Closed
icikle wants to merge 1 commit into
spring-projects:4.1.xfrom
icikle:fix/nested-jar-locking
Closed

icikle wants to merge 1 commit into
spring-projects:4.1.xfrom
icikle:fix/nested-jar-locking

Conversation

@icikle

@icikle icikle commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

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 - two CountDownLatches force one thread to hold an external lock while waiting on NestedJarFile'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 current synchronized (this) implementation hangs the JVM indefinitely. A jcmd <pid> Thread.print against the hung process confirms the exact deadlock:

Found one Java-level deadlock:
=============================
"holds-external-lock-then-wants-jar-monitor":
  waiting to lock monitor 0x0000000c8127b020 (object 0x00000007a06f3450, a org.springframework.boot.loader.jar.NestedJarFile),
  which is held by "holds-jar-monitor-then-wants-external-lock"

"holds-jar-monitor-then-wants-external-lock":
  waiting to lock monitor 0x0000000c8127af40 (object 0x00000007a06f34b0, a java.lang.Object),
  which is held by "holds-external-lock-then-wants-jar-monitor"

Java stack information for the threads listed above:
===================================================
"holds-external-lock-then-wants-jar-monitor":
	at org.springframework.boot.loader.jar.NestedJarFile.hasEntry(NestedJarFile.java:252)
	- waiting to lock <0x00000007a06f3450> (a org.springframework.boot.loader.jar.NestedJarFile)
	at org.springframework.boot.loader.jar.PreChangeNestedJarFileLockOrderingDeadlockTests.lambda$forceContentionForLock$0(PreChangeNestedJarFileLockOrderingDeadlockTests.java:61)
	- locked <0x00000007a06f34b0> (a java.lang.Object)
"holds-jar-monitor-then-wants-external-lock":
	at org.springframework.boot.loader.jar.PreChangeNestedJarFileLockOrderingDeadlockTests.lambda$forceContentionForLock$1(PreChangeNestedJarFileLockOrderingDeadlockTests.java:76)
	- waiting to lock <0x00000007a06f34b0> (a java.lang.Object)
	- locked <0x00000007a06f3450> (a org.springframework.boot.loader.jar.NestedJarFile)

Found 1 deadlock.

This is the same shape as the thread dumps in gh-51463 and gh-51379 - NestedJarFile.hasEntry() blocked waiting to lock a NestedJarFile/UrlNestedJarFile instance, 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 NestedJarFile locking on synchronized (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

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]>
@wilkinsona

Copy link
Copy Markdown
Member

The underlying defect is NestedJarFile locking on synchronized (this)

This isn't really our choice. NestedJarFile extends JarFile and we're following the locking model of JarFile and its super-class ZipFile. They declare various methods as synchronized and also uses synchronized(this) in places. We can't safely switch to an alternative locking approach in our sub-class without introducing unwanted inconsistencies under concurrent access.

Thanks anyway for the PR.

@wilkinsona wilkinsona closed this Sep 3, 2026
@wilkinsona wilkinsona added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. labels Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: declined A suggestion or change that we don't feel we should currently apply

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants