Skip to content

NullPointerException can be thrown when using uber jars due to a race condition in NestedJarFileResources - #51744

Merged
philwebb merged 2 commits into
spring-projects:4.0.xfrom
icikle:concurrency-fix-to-nestedjarfileresources
Sep 15, 2026
Merged

philwebb merged 2 commits into
spring-projects:4.0.xfrom
icikle:concurrency-fix-to-nestedjarfileresources

Conversation

@icikle

@icikle icikle commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Two related issues found while running a concurrency test targeting fixing gh-51463.

The methods getOrCreateInflater and endOrCacheInflater both synchronize on a local inflaterCache variable but attempted to use the inflaterCache instance which could be set to null.

The releaseInflators method had a synchronized block for ending each inflater but the cache was being nulled out outside the block meaning that the other 2 methods mentioned could hit a race condition resulting in a null pointer.

This change moves the nulling of the inflaterCache into the synchronized block.

See test NestedJarFileConcurrencyTests. This was written for the gh-51463 investigation and turned up this additional issue. With the previous version of the code running this test can fail due to a race condition. The failure observed in approximately 1/3 of runs was:

Unexpected failure during close race: java.lang.NullPointerException: Cannot invoke "java.util.Deque.add(Object)" because "this.inflaterCache" is null at org.springframework.boot.loader.jar.NestedJarFileResources.endOrCacheInflater(NestedJarFileResources.java:139)
java.lang.AssertionError: Unexpected failure during close race: java.lang.NullPointerException: Cannot invoke "java.util.Deque.add(Object)" because "this.inflaterCache" is null at org.springframework.boot.loader.jar.NestedJarFileResources.endOrCacheInflater(NestedJarFileResources.java:139)
	at org.springframework.boot.loader.jar.NestedJarFileConcurrencyTests.lambda$readUntilClosedOrLimit$0(NestedJarFileConcurrencyTests.java:217)
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.Deque.add(Object)" because "this.inflaterCache" is null
	at org.springframework.boot.loader.jar.NestedJarFileResources.endOrCacheInflater(NestedJarFileResources.java:139)

With the fix in place this test has not been observed to fail.

@icikle
icikle marked this pull request as ready for review September 14, 2026 21:45
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. label Sep 14, 2026
@philwebb philwebb changed the title Fix inflater cache race condition in NestedJarFileResources NullPointerException can be thrown when using Uber Jars due to a race condition in NestedJarFileResources Sep 15, 2026
@philwebb philwebb self-assigned this Sep 15, 2026
@philwebb philwebb added type: bug A general bug and removed status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. labels Sep 15, 2026
@philwebb philwebb added this to the 4.0.x milestone Sep 15, 2026
icikle and others added 2 commits September 14, 2026 17:34
Update `NestedJarFileResources` to fix a race condition that could
result in a `NullPointerException`.

The methods `getOrCreateInflater` and `endOrCacheInflater` both
synchronize on a local `inflaterCache` variable but attempted to use the
`this.inflaterCache` instance which could be set to null.

The `releaseInflators` method had a synchronized block for ending each
inflater but the cache was being nulled out outside the block meaning that
the other 2 methods mentioned could hit the race condition.

Signed-off-by: Ian Kettle <[email protected]>

See spring-projectsgh-51744
@philwebb
philwebb force-pushed the concurrency-fix-to-nestedjarfileresources branch from dd0f30a to d6aac54 Compare September 15, 2026 01:08
@philwebb
philwebb changed the base branch from 4.1.x to 4.0.x September 15, 2026 01:09
@philwebb
philwebb merged commit 9c851cf into spring-projects:4.0.x Sep 15, 2026
1 check failed
@philwebb philwebb modified the milestones: 4.0.x, 4.0.9 Sep 15, 2026
@philwebb

Copy link
Copy Markdown
Member

Thanks very much, I've merged the fix but decided not to merge the test right now since it's quite a lot of code.

@wilkinsona wilkinsona changed the title NullPointerException can be thrown when using Uber Jars due to a race condition in NestedJarFileResources NullPointerException can be thrown when using uber jars due to a race condition in NestedJarFileResources Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants