Skip to content

Commit 3cf22ac

Browse files
committed
ExternalFunctionInvokeNode: reacquire GIL only on exception path and transferToInterpreterAndInvalidate
1 parent d867023 commit 3cf22ac

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ThreadModuleBuiltins.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ long start(VirtualFrame frame, Object cls, Object callable, Object args, Object
230230
// TODO: python thread stack size != java thread stack size
231231
// ignore setting the stack size for the moment
232232
TruffleThreadBuilder threadBuilder = env.newTruffleThreadBuilder(() -> {
233-
GilNode.UncachedAcquire gil = GilNode.uncachedAcquire();
234-
try {
233+
try (GilNode.UncachedAcquire gil = GilNode.uncachedAcquire()) {
235234
// the increment is protected by the gil
236235
int curCount = (int) HiddenAttr.ReadNode.executeUncached(threadModule, THREAD_COUNT, 0);
237236
HiddenAttr.WriteNode.executeUncached(threadModule, THREAD_COUNT, curCount + 1);
@@ -252,8 +251,6 @@ long start(VirtualFrame frame, Object cls, Object callable, Object args, Object
252251
curCount = (int) HiddenAttr.ReadNode.executeUncached(threadModule, THREAD_COUNT, 1);
253252
HiddenAttr.WriteNode.executeUncached(threadModule, THREAD_COUNT, curCount - 1);
254253
}
255-
} finally {
256-
gil.close();
257254
}
258255
}).context(env.getContext()).threadGroup(context.getThreadGroup());
259256

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -851,15 +851,17 @@ static Object invoke(VirtualFrame frame, Node inliningTarget, PythonContext ctx,
851851
} catch (ArityException e) {
852852
CompilerDirectives.transferToInterpreterAndInvalidate();
853853
throw PRaiseNode.raiseUncached(inliningTarget, TypeError, ErrorMessages.CALLING_NATIVE_FUNC_EXPECTED_ARGS, name, e.getExpectedMinArity(), e.getActualArity());
854-
} finally {
855-
CApiTiming.exit(timing);
854+
} catch (Throwable exception) {
856855
/*
857856
* Always re-acquire the GIL here. This is necessary because it could happen that C
858857
* extensions are releasing the GIL and if then an LLVM exception occurs, C code
859858
* wouldn't re-acquire it (unexpectedly).
860859
*/
861-
gilNode.acquire(ctx);
862-
860+
CompilerDirectives.transferToInterpreterAndInvalidate();
861+
GilNode.uncachedAcquire();
862+
throw exception;
863+
} finally {
864+
CApiTiming.exit(timing);
863865
/*
864866
* Special case after calling a C function: transfer caught exception back to frame
865867
* to simulate the global state semantics.

0 commit comments

Comments
 (0)