Repository navigation
Clean up the nested jar temp dir without a shutdown hook - #9928
Merged
Merged
Conversation
getTempDir() registered a shutdown hook that deletes the temp dir and the files in it. The hook is an anonymous Thread subclass, so ApplicationShutdownHooks kept the class loader that loaded JRuby alive until the JVM exited, for the first JRuby that created the directory. close() already deletes the copied jars when the runtime is torn down. A copy it can't delete, as on Windows while the jar is still open, is now registered with deleteOnExit. So are the native libraries DynamicLibrary extracts, since they stay loaded for the life of that JRuby. The directory itself is registered when it's created, and is deleted at exit if it's empty. Unlike the hook, this doesn't delete the copied jars of a runtime that is never torn down.
7 of 8 tasks
headius
approved these changes
Oct 29, 2026
headius
approved these changes
Oct 29, 2026
headius
left a comment
Member
There was a problem hiding this comment.
I'd like to see the described test scenarios as unit tests but merging now to get this into a jruby-head build for broader verification.
Member
|
I stared at the merge button for a long time wondering if the restoration of the I finally realized that Windows gains a small leak there but removes the much larger leak of JRuby classloaders and exit hooks. So this is still a net win on Windows (the best we can hope for right now given Windows' jar-deletion issues). |
headius
pushed a commit
that referenced
this pull request
Oct 30, 2026
Covers the scenarios from #9928: close() deletes the jars it copied, copying a nested jar doesn't keep the class loader that loaded JRuby alive, and a copy close() can't delete is deleted at exit, along with the temp dir. The last one runs in a child JVM that makes the temp dir read-only, so it needs a file system with POSIX permissions.
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.
Part of #9781, against
jruby-10.0for the 10.0 LTS line. This is option 4 from your comment.JRubyClassLoader.getTempDir()registered a shutdown hook that deletes the temp dir for nested jars and everything in it. The hook is an anonymousThreadsubclass,JRubyClassLoader$1, soApplicationShutdownHookskept the class loader that loaded JRuby alive until the JVM exited. Only the JRuby that creates the directory registers it, so it pinned the first of these class loaders in a JVM.This removes the hook:
close()already deletes each copied jar when the runtime is torn down. A copy it can't delete, as on Windows while the jar is still open, is now registered withdeleteOnExit. On Unix the delete succeeds and nothing is registered, soDeleteOnExitHookdoesn't grow with every runtime, which is what Use shutdown hook to delete temp files instead of File.deleteOnExit #4578 was about.DynamicLibraryextracts native libraries into the same directory. They stay loaded and cached for the life of that JRuby, so they can't be deleted at teardown. Each one is registered withdeleteOnExitwhen it's extracted, which is one entry per library per JRuby class loader.deleteOnExitwhen it's created. That happens once per JVM, since every JRuby uses the samejruby-<pid>directory. It's registered before the files in it, so it's deleted after them, if it's empty by then.I kept the cleanup in
close()instead of handing it to a Cleaner. Deleting a few files is quick, and a pending Cleaner action would keep the class loader alive until it runs.One difference from the hook: if a runtime is never torn down, for example after
exit!orSystem.exit, its copied jars and the directory stay behind after the JVM exits. Extracted native libraries are still deleted.Before and after. The reproducer from #9781 doesn't copy any nested jars, so I used a variant in which each runtime adds a jar that's nested in another jar and opens a native library from inside a jar. With JRuby 10.0, #9927 and jffi 1.4.4, and
-XX:SoftRefLRUPolicyMSPerMB=0:jruby-10.0To check the fallback, I removed write permission from the temp dir before
close(), so the delete failed. The copy was registered withdeleteOnExit, and it and the directory were gone after the JVM exited.rake spec:jipasses (974 examples, 0 failures, 9 pending).rake spec:ruby:fasthas 1 failure and 4 errors, all IPv6 socket specs that fail the same way on unmodifiedjruby-10.0, because my machine has no IPv6 network.