Do not close cached JarFile obtained from JarURLConnection - #4542
Closed
aayushsrivastava wants to merge 1 commit into
Closed
Do not close cached JarFile obtained from JarURLConnection#4542aayushsrivastava wants to merge 1 commit into
aayushsrivastava wants to merge 1 commit into
Conversation
Closing the shared cached instance breaks concurrent readers and classloading, e.g. Spring Boot nested jars. Closes elastic#4541.
|
❌ Author of the following commits did not sign a Contributor Agreement: Please, read and sign the above mentioned agreement if you want to contribute to this project |
🤖 GitHub commentsJust comment with:
|
Author
|
Not needed anymore since #4543 fixes the same problem |
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.
What does this PR do?
CustomElementMatchers#readImplementationVersioncloses theJarFileit uses to read a library's version unconditionally in itsfinallyblock. When the class'sProtectionDomainresolves to aJarURLConnection(the common case),JarURLConnection#getJarFile()returns a shared, cachedJarFileinstance by default (caching is on unlesssetUseCaches(false)was called), and per its contract that instance must not be closed by the caller.Closing it breaks every other concurrent reader/classloader backed by the same cached
JarFile. On Spring Boot executable jars this is severe: Boot'snested:protocol returns its shared cachedNestedJarFilefor a dependency jar, and that same instance also backs classloading for that jar. Closing it during a version-gated instrumentation check (e.g. the Mongo driver check) can fail concurrent classloads withNoClassDefFoundErrorand reads withZipException: ZipFile closed, causing an intermittent startup crash.Fixes #4541
This PR tracks whether the connection actually owns a private (non-cached)
JarFileviaJarURLConnection#getUseCaches(), and only closes it in that case. Thenew JarFile(file)branch (non-JarURLConnectioncase) is unaffected and is still always closed.Checklist
Note: only compiled
apm-agent-plugin-sdklocally (./mvnw -pl apm-agent-plugin-sdk -am compile/test-compile, both clean); did not run the full test/verify suite locally, so CI should be the source of truth there.