Skip to content

Test the nested jar temp dir cleanup - #9945

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

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

Conversation

@matrei

@matrei matrei commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #9928, with its test scenarios as unit tests, as you asked in your review. Part of #9781, against jruby-10.0 for the 10.0 LTS line.

This adds JRubyClassLoaderTest, which builds a jar with another jar in it in a temp folder, so there's no binary fixture:

  • closeDeletesCopiedJars: adding the nested jar's jar:…!/inner.jar URL copies it into the temp dir, and close() deletes the copy.
  • tempDirDoesNotKeepTheClassLoaderAlive: loads JRubyClassLoader in a disposable class loader, copies the nested jar and calls close(), then checks that the class loader is collected. It sets jruby.ji.nested.jar.tmpdir to a new folder while it runs. The hook was only registered by the JRuby that created the directory, so without that, the test could pass on the old code whenever another test had already created jruby-<pid>.
  • leftoversAreDeletedAtExit: a child JVM copies the nested jar and calls close() while the temp dir is read-only, so the copy can't be deleted, as on Windows while the jar is still open. After the child exits, the test checks that the copy and the directory are both gone. It needs POSIX file permissions, so it's skipped on Windows, and also when close() can delete the copy anyway, for example when running as root.

To check that the tests can fail, I ran them against changed versions of JRubyClassLoader:

JRubyClassLoader fails
as in #9928 none (3 runs)
before #9928, with the shutdown hook tempDirDoesNotKeepTheClassLoaderAlive
without deleteOnExit for a copy close() can't delete leftoversAreDeletedAtExit
without deleteOnExit for the temp dir leftoversAreDeletedAtExit

closeDeletesCopiedJars and leftoversAreDeletedAtExit also pass with the shutdown hook, which deleted everything too. They check that #9928 kept that behaviour. Like ScriptingContainerClassLeakTest, tempDirDoesNotKeepTheClassLoaderAlive relies on System.gc().

Native libraries that DynamicLibrary extracts from a jar aren't covered. That would need a real native library for each platform.

./mvnw -pl core -Ptest -Dtest=JRubyClassLoaderTest test runs them.

Covers the scenarios from jruby#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.

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

Great, thanks.

@headius
headius merged commit f709238 into jruby:jruby-10.0 Oct 30, 2026
127 checks passed
@matrei
matrei deleted the test/nested-jar-temp-cleanup branch October 30, 2026 08:12
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