Skip to content

Commit e1a60b1

Browse files
committed
don't reference the PythonContext from daemon threads via the AsyncHandlers
1 parent 25d722f commit e1a60b1

File tree

1 file changed

+10
-5
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime

1 file changed

+10
-5
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.runtime;
4242

43+
import java.lang.ref.WeakReference;
4344
import java.util.Arrays;
4445
import java.util.concurrent.ConcurrentLinkedQueue;
4546
import java.util.concurrent.Executors;
@@ -134,7 +135,7 @@ public Thread newThread(Runnable r) {
134135
}
135136
});
136137

137-
private final PythonContext context;
138+
private final WeakReference<PythonContext> context;
138139
private final ConcurrentLinkedQueue<AsyncAction> scheduledActions = new ConcurrentLinkedQueue<>();
139140
private volatile boolean hasScheduledAction = false;
140141
private final Lock executingScheduledActions = new ReentrantLock();
@@ -215,7 +216,7 @@ public boolean isInternal() {
215216
private final RootCallTarget callTarget;
216217

217218
AsyncHandler(PythonContext context) {
218-
this.context = context;
219+
this.context = new WeakReference<>(context);
219220
this.callTarget = PythonUtils.getOrCreateCallTarget(new CallRootNode(context.getLanguage()));
220221
}
221222

@@ -230,12 +231,12 @@ void registerAction(Supplier<AsyncAction> actionSupplier) {
230231
void triggerAsyncActions(VirtualFrame frame, BranchProfile actionProfile) {
231232
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.SLOWPATH_PROBABILITY, hasScheduledAction)) {
232233
actionProfile.enter();
233-
IndirectCallContext.enter(frame, context, null);
234+
IndirectCallContext.enter(frame, context.get(), null);
234235
try {
235236
CompilerDirectives.transferToInterpreter();
236237
processAsyncActions();
237238
} finally {
238-
IndirectCallContext.exit(frame, context, null);
239+
IndirectCallContext.exit(frame, context.get(), null);
239240
}
240241
}
241242
}
@@ -273,13 +274,17 @@ void triggerAsyncActions(VirtualFrame frame, BranchProfile actionProfile) {
273274
* for weakref finalizers, 1 for signals, 1 for destructors).
274275
*/
275276
private void processAsyncActions() {
277+
PythonContext ctx = context.get();
278+
if (ctx == null) {
279+
return;
280+
}
276281
if (executingScheduledActions.tryLock()) {
277282
hasScheduledAction = false;
278283
try {
279284
ConcurrentLinkedQueue<AsyncAction> actions = scheduledActions;
280285
AsyncAction action;
281286
while ((action = actions.poll()) != null) {
282-
action.execute(context);
287+
action.execute(ctx);
283288
}
284289
} finally {
285290
executingScheduledActions.unlock();

0 commit comments

Comments
 (0)