Skip to content

Do not close cached JarFile obtained from JarURLConnection - #4542

Closed
aayushsrivastava wants to merge 1 commit into
elastic:mainfrom
aayushsrivastava:fix/jarfile-close-cached-connection
Closed

Do not close cached JarFile obtained from JarURLConnection#4542
aayushsrivastava wants to merge 1 commit into
elastic:mainfrom
aayushsrivastava:fix/jarfile-close-cached-connection

Conversation

@aayushsrivastava

Copy link
Copy Markdown

What does this PR do?

CustomElementMatchers#readImplementationVersion closes the JarFile it uses to read a library's version unconditionally in its finally block. When the class's ProtectionDomain resolves to a JarURLConnection (the common case), JarURLConnection#getJarFile() returns a shared, cached JarFile instance by default (caching is on unless setUseCaches(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's nested: protocol returns its shared cached NestedJarFile for 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 with NoClassDefFoundError and reads with ZipException: ZipFile closed, causing an intermittent startup crash.

Fixes #4541

This PR tracks whether the connection actually owns a private (non-cached) JarFile via JarURLConnection#getUseCaches(), and only closes it in that case. The new JarFile(file) branch (non-JarURLConnection case) is unaffected and is still always closed.

Checklist

  • This is an enhancement of existing features, or a new feature in existing plugins
    • I have updated CHANGELOG.next-release.md
    • I have added tests that prove my fix is effective or that my feature works
    • Added an API method or config option? Document in which version this will be introduced
    • I have made corresponding changes to the documentation
  • This is a bugfix
  • This is a new plugin
    • I have updated CHANGELOG.next-release.md
    • My code follows the style guidelines of this project
    • I have made corresponding changes to the documentation
    • I have added tests that prove my fix is effective or that my feature works
    • New and existing unit tests pass locally with my changes
    • I have updated supported-technologies.md
    • Added an API method or config option? Document in which version this will be introduced
    • Added an instrumentation plugin? Describe how you made sure that old, non-supported versions are not instrumented by accident.
  • This is something else

Note: only compiled apm-agent-plugin-sdk locally (./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.

Closing the shared cached instance breaks concurrent readers and classloading, e.g. Spring Boot nested jars. Closes elastic#4541.
@aayushsrivastava
aayushsrivastava requested a review from a team as a code owner August 14, 2026 14:22
@cla-checker-service

Copy link
Copy Markdown

❌ Author of the following commits did not sign a Contributor Agreement:
f808b89

Please, read and sign the above mentioned agreement if you want to contribute to this project

@github-actions

Copy link
Copy Markdown

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)

@github-actions github-actions Bot added community Issues and PRs created by the community triage labels Aug 14, 2026
@aayushsrivastava

Copy link
Copy Markdown
Author

Not needed anymore since #4543 fixes the same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-java community Issues and PRs created by the community triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CustomElementMatchers.readImplementationVersion closes a shared cached JarFile, crashing Spring Boot executable jar startups

1 participant