Skip to content

Clean up the nested jar temp dir without a shutdown hook - #9928

Merged
headius merged 1 commit into
jruby:jruby-10.0from
matrei:fix/nested-jar-temp-cleanup
Oct 29, 2026
Merged

headius merged 1 commit into
jruby:jruby-10.0from
matrei:fix/nested-jar-temp-cleanup

Conversation

@matrei

@matrei matrei commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Part of #9781, against jruby-10.0 for 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 anonymous Thread subclass, JRubyClassLoader$1, so ApplicationShutdownHooks kept 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 with deleteOnExit. On Unix the delete succeeds and nothing is registered, so DeleteOnExitHook doesn't grow with every runtime, which is what Use shutdown hook to delete temp files instead of File.deleteOnExit #4578 was about.
  • DynamicLibrary extracts 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 with deleteOnExit when it's extracted, which is one entry per library per JRuby class loader.
  • The directory itself is registered with deleteOnExit when it's created. That happens once per JVM, since every JRuby uses the same jruby-<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! or System.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:

class loaders alive after 4 rounds shutdown hooks from them copied jars after teardown temp dir after exit
jruby-10.0 1 of 4, the first 1 0 deleted
this PR 0 of 4 0 0 deleted

To check the fallback, I removed write permission from the temp dir before close(), so the delete failed. The copy was registered with deleteOnExit, and it and the directory were gone after the JVM exited.

rake spec:ji passes (974 examples, 0 failures, 9 pending). rake spec:ruby:fast has 1 failure and 4 errors, all IPv6 socket specs that fail the same way on unmodified jruby-10.0, because my machine has no IPv6 network.

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.

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@headius
headius merged commit 445df73 into jruby:jruby-10.0 Oct 29, 2026
126 of 127 checks passed
@headius

headius commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

I stared at the merge button for a long time wondering if the restoration of the deleteOnExit leak on Windows was worth the ClassLoader fix.

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).

@matrei
matrei deleted the fix/nested-jar-temp-cleanup branch October 29, 2026 06:41
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants