Skip to content

Fix runtime and classloader retention after ScriptingContainer terminate#9315

Open
jimtng wants to merge 3 commits intojruby:jruby-10.0from
jimtng:mem-leak
Open

Fix runtime and classloader retention after ScriptingContainer terminate#9315
jimtng wants to merge 3 commits intojruby:jruby-10.0from
jimtng:mem-leak

Conversation

@jimtng
Copy link
Copy Markdown
Contributor

@jimtng jimtng commented Mar 17, 2026

Clear lingering ThreadContext soft references during teardown, drop the embed LocalContext runtime reference, and avoid re-adopting the current thread while Ruby is shutting down.

Also switch Java integration class caches to weak values and avoid recreating JavaSupport during teardown so terminated runtimes and their JRubyClassLoaders can be collected promptly.

Add regression coverage for same-thread and worker-thread container termination.

Fixes #9092, Fixes #9070, Fixes openhab/openhab-jruby#520


Note: I asked an AI tool to help identify this issue. I verified the result on a patched JRuby 10.0.4.0 running inside openHAB, where the memory leak had previously been observed. After applying the fix, the GC consistently returned the heap to its original size, even after repeated script‑engine creation and teardown cycles.

Clear lingering ThreadContext soft references during teardown, drop
the embed LocalContext runtime reference, and avoid re-adopting the
current thread while Ruby is shutting down.

Also switch Java integration class caches to weak values and avoid
recreating JavaSupport during teardown so terminated runtimes and
their JRubyClassLoaders can be collected promptly.

Add regression coverage for same-thread and worker-thread container
termination.

Fixes jrubyGH-9092

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
@jimtng jimtng changed the base branch from master to jruby-10.0 March 21, 2026 06:09
@headius
Copy link
Copy Markdown
Member

headius commented Mar 28, 2026

There's clearly some issues here but I'd like to figure out how to land a fix.

It seems that the weak references to the class values is too weak, and I'm not sure we should be using weak references at this point anyway. If they're weak and a JavaClass goes unreferenced for a couple of GC cycles, it will end up getting collected and a future access will cause it to be recreated. That's the source of all the reinitialized constant warnings and the reason the test/jruby tests fail here.

Even a SoftReference is probably too weak, since it just delays the time until the JavaClass gets collected.

Is that part of the patch necessary for this to work? Is clearing the reference to the JavaSupport class not enough?

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
@jimtng
Copy link
Copy Markdown
Contributor Author

jimtng commented Mar 29, 2026

Is that part of the patch necessary for this to work?

Yes it's necessary.

Reverting only the JavaSupport/ClassValue part causes all three GH-9092 regression tests in ScriptingContainerClassLeakTest to fail, and restoring it makes that test class pass again. So that part is necessary for the leak fix, not for normal runtime cache behavior.

If you have a cleaner lifecycle-based alternative, I’m happy to switch to that.

@headius
Copy link
Copy Markdown
Member

headius commented Mar 29, 2026

My question was really if we need both changes. Clearing the reference to the JavaSupport instance seems like it would be sufficient, and making the Java class references weak leads to other problems quickly.

I can give it a try myself when I'm in the office.

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
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.

Memory leak in JRuby ThreadLocal Memory Leak in JRuby 10.0.2.0 with ScriptingContainer Memory leak in jruby within openhab

2 participants