Skip to content

Commit 0a0ec5e

Browse files
committed
Guard C API polling by thread state
1 parent 7816da4 commit 0a0ec5e

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions/CApiTransitions.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,22 @@ public static int pollReferenceQueue() {
454454
if (handleContext.referenceQueuePollingState != RQ_READY) {
455455
return manuallyCollected;
456456
}
457+
/*
458+
* Polling the reference queue may deallocate native GC objects and therefore re-enter
459+
* native code paths that use '_PyThreadState_GET()' to obtain the current thread's GC
460+
* state. So, we may only poll once the current thread has installed its native
461+
* 'tstate_current' pointer.
462+
*/
463+
if (!context.getThreadState(context.getLanguage()).isNativeThreadStateInitialized()) {
464+
return manuallyCollected;
465+
}
457466
try (GilNode.UncachedAcquire ignored = GilNode.uncachedAcquire()) {
458467
if (handleContext.referenceQueuePollingState != RQ_READY) {
459468
return manuallyCollected;
460469
}
470+
if (!context.getThreadState(context.getLanguage()).isNativeThreadStateInitialized()) {
471+
return manuallyCollected;
472+
}
461473
ReferenceQueue<Object> queue = handleContext.referenceQueue;
462474
int count = 0;
463475
long start = 0;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,10 @@ public void setNativeThreadLocalVarPointer(Object ptr) {
592592
String.format("ptr = %s; nativeThreadLocalVarPointer = %s", ptr, nativeThreadLocalVarPointer);
593593
this.nativeThreadLocalVarPointer = ptr;
594594
}
595+
596+
public boolean isNativeThreadStateInitialized() {
597+
return nativeThreadLocalVarPointer != null;
598+
}
595599
}
596600

597601
private static final class AtExitHook {

0 commit comments

Comments
 (0)