Skip to content

Commit 9f72b0c

Browse files
committed
Clear references to threads and thread contexts on teardown.
This change clears the following on JRuby runtime teardown: * The thread-local context reference in ThreadService (dereferenced entirely) * The mainContext reference in ThreadService * The mapping from native threads to RubyThread instances. This should clear all JRuby-specific runtime data attached to both Ruby and adopted Java threads. Fixes #3313. Supersedes #3316.
1 parent 081d774 commit 9f72b0c

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

core/src/main/java/org/jruby/Ruby.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3370,8 +3370,6 @@ public void tearDown(boolean systemExit) {
33703370
}
33713371
}
33723372

3373-
getThreadService().disposeCurrentThread();
3374-
33753373
getBeanManager().unregisterCompiler();
33763374
getBeanManager().unregisterConfig();
33773375
getBeanManager().unregisterParserStats();
@@ -3388,6 +3386,8 @@ public void tearDown(boolean systemExit) {
33883386
printProfileData(profileCollection);
33893387
}
33903388

3389+
getThreadService().teardown();
3390+
33913391
if (systemExit && status != 0) {
33923392
throw newSystemExit(status);
33933393
}

core/src/main/java/org/jruby/RubyThread.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.nio.channels.Selector;
4242
import java.util.Collections;
4343
import java.util.Iterator;
44+
import java.util.Optional;
4445
import java.util.Queue;
4546
import java.util.Vector;
4647
import java.util.WeakHashMap;
@@ -382,6 +383,7 @@ public void setContext(ThreadContext context) {
382383
}
383384

384385
public ThreadContext getContext() {
386+
WeakReference<ThreadContext> contextRef = this.contextRef;
385387
return contextRef == null ? null : contextRef.get();
386388
}
387389

core/src/main/java/org/jruby/internal/runtime/ThreadService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public class ThreadService {
121121
* through ThreadContext instances every time a Java thread enters and exits
122122
* Ruby space.
123123
*/
124-
private final ThreadLocal<SoftReference<ThreadContext>> localContext;
124+
private ThreadLocal<SoftReference<ThreadContext>> localContext;
125125

126126
/**
127127
* The Java thread group into which we register all Ruby threads. This is
@@ -160,6 +160,17 @@ public void disposeCurrentThread() {
160160
rubyThreadMap.remove(Thread.currentThread());
161161
}
162162

163+
public void teardown() {
164+
// clear all thread-local context references
165+
localContext = new ThreadLocal<SoftReference<ThreadContext>>();
166+
167+
// clear main context reference
168+
mainContext = null;
169+
170+
// clear thread map
171+
rubyThreadMap.clear();
172+
}
173+
163174
public void initMainThread() {
164175
this.mainContext = ThreadContext.newContext(runtime);
165176

0 commit comments

Comments
 (0)