40
40
*/
41
41
package com .oracle .graal .python .runtime ;
42
42
43
+ import java .lang .ref .WeakReference ;
43
44
import java .util .Arrays ;
44
45
import java .util .concurrent .ConcurrentLinkedQueue ;
45
46
import java .util .concurrent .Executors ;
@@ -134,7 +135,7 @@ public Thread newThread(Runnable r) {
134
135
}
135
136
});
136
137
137
- private final PythonContext context ;
138
+ private final WeakReference < PythonContext > context ;
138
139
private final ConcurrentLinkedQueue <AsyncAction > scheduledActions = new ConcurrentLinkedQueue <>();
139
140
private volatile boolean hasScheduledAction = false ;
140
141
private final Lock executingScheduledActions = new ReentrantLock ();
@@ -215,7 +216,7 @@ public boolean isInternal() {
215
216
private final RootCallTarget callTarget ;
216
217
217
218
AsyncHandler (PythonContext context ) {
218
- this .context = context ;
219
+ this .context = new WeakReference <>( context ) ;
219
220
this .callTarget = PythonUtils .getOrCreateCallTarget (new CallRootNode (context .getLanguage ()));
220
221
}
221
222
@@ -230,12 +231,12 @@ void registerAction(Supplier<AsyncAction> actionSupplier) {
230
231
void triggerAsyncActions (VirtualFrame frame , BranchProfile actionProfile ) {
231
232
if (CompilerDirectives .injectBranchProbability (CompilerDirectives .SLOWPATH_PROBABILITY , hasScheduledAction )) {
232
233
actionProfile .enter ();
233
- IndirectCallContext .enter (frame , context , null );
234
+ IndirectCallContext .enter (frame , context . get () , null );
234
235
try {
235
236
CompilerDirectives .transferToInterpreter ();
236
237
processAsyncActions ();
237
238
} finally {
238
- IndirectCallContext .exit (frame , context , null );
239
+ IndirectCallContext .exit (frame , context . get () , null );
239
240
}
240
241
}
241
242
}
@@ -273,13 +274,17 @@ void triggerAsyncActions(VirtualFrame frame, BranchProfile actionProfile) {
273
274
* for weakref finalizers, 1 for signals, 1 for destructors).
274
275
*/
275
276
private void processAsyncActions () {
277
+ PythonContext ctx = context .get ();
278
+ if (ctx == null ) {
279
+ return ;
280
+ }
276
281
if (executingScheduledActions .tryLock ()) {
277
282
hasScheduledAction = false ;
278
283
try {
279
284
ConcurrentLinkedQueue <AsyncAction > actions = scheduledActions ;
280
285
AsyncAction action ;
281
286
while ((action = actions .poll ()) != null ) {
282
- action .execute (context );
287
+ action .execute (ctx );
283
288
}
284
289
} finally {
285
290
executingScheduledActions .unlock ();
0 commit comments