-
Notifications
You must be signed in to change notification settings - Fork 6
Feature: Refactor plugin loading #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
infeo
wants to merge
6
commits into
develop
Choose a base branch
from
feature/refactor-plugin-loading
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+155
−23
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
168cd1c
Only search directories, if pluginDir property is set and valid
infeo 8b5b3ce
Cache URLClassLoader if created with system property
infeo 95cff98
improve logging
infeo 3d11f8e
simplify caching
infeo 5d1ddb8
Return new ClassLoader if property changes
infeo 2613fc5
Rework cache impl
infeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
cat -n src/main/java/org/cryptomator/integrations/common/ClassLoaderFactory.java | head -100Repository: cryptomator/integrations-api
Length of output: 4342
🏁 Script executed:
grep -n "CACHE\|close\|URLClassLoader" src/main/java/org/cryptomator/integrations/common/ClassLoaderFactory.javaRepository: cryptomator/integrations-api
Length of output: 1069
🌐 Web query:
URLClassLoader close resource leak semantics Java💡 Result:
URLClassLoader.close()is specified to do: It closes the class loader so it can no longer load new classes/resources, while parent-loader classes/resources remain accessible; and forjar:/file:URLs it closes any files it opened (e.g.,JarFilehandles). Callingclose()more than once is a no-op. [1]JarFilehandle commonly shows up as an inability to delete/replace the JAR until the loader (and any streams) are closed. Oracle explicitly documents Java 7’sclose()as the mechanism to allow replacing an updated JAR by closing the old loader and creating a new one. [2]URLClassLoader.close()did not close all underlying JAR/resource handles in some resource-loading paths (notably involvinggetResource*/getResourceAsStream), leaving JARs locked even afterclose()—tracked in issues like JDK-7183373 and related bugs. [3]jdk.internal.loader.URLClassPathleakingJarFileinstances in certain manifestClass-Pathscenarios—JDK-8338445 discussion). [4]Practical guidance: treat a
URLClassLoaderas an owned resource—use try-with-resources (or ensureclose()infinally), and also close everyInputStreamyou obtain fromgetResourceAsStream(); if you hit jar-locking despite that, check for known JDK issues in your exact Java version and consider workarounds (e.g., disabling JAR URL caching in the specific code path) based on the affected bug report. [1][3]Sources:
[1] Java SE 21
URLClassLoaderJavadoc (close semantics, closes opened jar/file handles, idempotent) (docs.oracle.com)[2] Oracle “Closing a URLClassLoader” guide (Java 7 close enables replacing updated JARs; loader invalidated) (docs.oracle.com)
[3] OpenJDK bug JDK-7183373 (close not closing JARs in some resource-loading scenarios; related issues) (bugs.openjdk.org)
[4] OpenJDK core-libs-dev thread on JDK-8338445 (
URLClassPathpotentialJarFileleak) (mail.openjdk.org)Citations:
Close the previous cached class loader when rotating the cache.
Line 43 overwrites
CACHEwithout closing the oldURLClassLoader. SinceURLClassLoadermanages open JAR file handles, this leaks file handles and can lock plugin JARs after repeated changes to thecryptomator.pluginDirsystem property. On platforms like Windows, this prevents deletion or replacement of updated plugin files.♻️ Suggested fix
public synchronized static URLClassLoader forPluginDirFromCache() { String currentPluginDir = System.getProperty(PLUGIN_DIR_KEY); if (CACHE == null || !Objects.equals(CACHE.pluginDir, currentPluginDir)) { - CACHE = new Cache(currentPluginDir, forPluginDirInternal(currentPluginDir)); + var previous = CACHE == null ? null : CACHE.classLoader; + CACHE = new Cache(currentPluginDir, forPluginDirInternal(currentPluginDir)); + closeQuietly(previous); } return CACHE.classLoader; } + +private static void closeQuietly(`@Nullable` URLClassLoader classLoader) { + if (classLoader == null) { + return; + } + try { + classLoader.close(); + } catch (IOException e) { + LOG.warn("Failed to close previous plugin class loader.", e); + } +}🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class loader is used in the service loader. Closing it can lead to failures during service discovery.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@infeo, that's a fair and important point.URLClassLoader.close()invalidates the loader so it can no longer load new classes or resources, and sinceServiceLoaderlazily discovers and instantiates providers, closing the old loader while it (or any service it loaded) is still in use could causeClassNotFoundExceptionor other failures at runtime. Accepting the file handle tradeoff to avoid breaking active service discovery makes sense here.I'll withdraw the suggestion.
✏️ Learnings added
🧠 Learnings used