Skip to content

Commit 388de48

Browse files
committed
Cleanup Java mirror of thread state pointer for thread disposal in PyGILState_Release
1 parent 540ccd7 commit 388de48

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

graalpython/com.oracle.graal.python.cext/src/pystate.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ PyGILState_Release(PyGILState_STATE oldstate)
177177
{
178178
if (oldstate == PyGILState_UNLOCKED) {
179179
GraalPyTruffleGILState_Release();
180+
/* In the GraalPyTruffleGILState_Release up-call, we cleaned-up the pointer saved in
181+
* Java level Python thread state to avoid setting it to NULL in Truffle thread disposal
182+
* code, because it is not clear if the native thread is guaranteed to still be around
183+
* when the Truffle thread is being disposed.
184+
*/
185+
tstate_current = NULL;
180186
}
181187
if (TRUFFLE_CONTEXT) {
182188
graalpy_gilstate_counter--;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextPyStateBuiltins.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.Py_ssize_t;
5151
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.Void;
5252

53+
import com.oracle.graal.python.PythonLanguage;
5354
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiBuiltin;
5455
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiNullaryBuiltinNode;
5556
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiUnaryBuiltinNode;
@@ -102,6 +103,10 @@ abstract static class PyTruffleGILState_Release extends CApiNullaryBuiltinNode {
102103
static Object restore(
103104
@Cached GilNode gil) {
104105
gil.release(true);
106+
// The C API that makes this up-call should reset the thread local variable to NULL
107+
// and here, on the Java side, we can clean up the pointers, because there is no need
108+
// to reset the native thread local on Truffle thread disposal
109+
getContext(gil).getThreadState(PythonLanguage.get(gil)).resetNativeThreadLocalVarPointer();
105110
return PNone.NO_VALUE;
106111
}
107112
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,11 @@ public void setAsyncgenFirstIter(Object asyncgenFirstIter) {
636636
this.asyncgenFirstIter = asyncgenFirstIter;
637637
}
638638

639+
public void resetNativeThreadLocalVarPointer() {
640+
nativeThreadLocalVarRawPointer = 0;
641+
nativeThreadLocalVarPointer = null;
642+
}
643+
639644
public void setNativeThreadLocalVarPointer(InteropLibrary interop, Object ptr) {
640645
// either unset or same
641646
assert nativeThreadLocalVarPointer == null || nativeThreadLocalVarPointer == ptr ||

0 commit comments

Comments
 (0)