Skip to content

Commit e7f2217

Browse files
timfelotethal
authored andcommitted
kill python threads during shutdown with a safepoint action
1 parent 5be1af3 commit e7f2217

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
102102
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
103103
import com.oracle.truffle.api.ContextThreadLocal;
104+
import com.oracle.truffle.api.ThreadLocalAction;
104105
import com.oracle.truffle.api.Truffle;
105106
import com.oracle.truffle.api.TruffleFile;
106107
import com.oracle.truffle.api.TruffleLanguage;
@@ -1154,7 +1155,12 @@ private void joinThreads() {
11541155
// in the acquireGil function, which will be interrupted for these threads
11551156
disposeThread(thread);
11561157
for (int i = 0; i < 100 && thread.isAlive(); i++) {
1157-
thread.interrupt();
1158+
env.submitThreadLocal(new Thread[]{thread}, new ThreadLocalAction(true, false) {
1159+
@Override
1160+
protected void perform(ThreadLocalAction.Access access) {
1161+
throw new PythonThreadKillException();
1162+
}
1163+
});
11581164
thread.join(2);
11591165
}
11601166
if (thread.isAlive()) {
@@ -1263,25 +1269,10 @@ boolean tryAcquireGil() {
12631269
@TruffleBoundary
12641270
void acquireGil() throws InterruptedException {
12651271
assert !ownsGil() : dumpStackOnAssertionHelper("trying to acquire the GIL more than once");
1266-
try {
1267-
globalInterpreterLock.lockInterruptibly();
1268-
} catch (InterruptedException e) {
1269-
if (!ImageInfo.inImageBuildtimeCode() && getThreadState(getLanguage()).isShuttingDown()) {
1270-
// This is a thread being killed during normal context shutdown. This thread
1271-
// should exit now. This should usually only happen for daemon threads on
1272-
// context shutdown. This is the equivalent to the logic in pylifecycle.c and
1273-
// PyEval_RestoreThread which, on Python shutdown, will join non-daemon threads
1274-
// and then simply start destroying the thread states of remaining threads. If
1275-
// any remaining daemon thread then tries to acquire the GIL, it'll notice the
1276-
// shutdown is happening and exit.
1277-
throw new PythonThreadKillException();
1278-
} else {
1279-
// We are being interrupted through some non-internal means. Commonly this may be
1280-
// because Truffle wants to run some safepoint action. In this case, we need to
1281-
// rethrow the InterruptedException.
1282-
Thread.interrupted();
1283-
throw e;
1284-
}
1272+
boolean wasInterrupted = Thread.interrupted();
1273+
globalInterpreterLock.lockInterruptibly();
1274+
if (wasInterrupted) {
1275+
Thread.currentThread().interrupt();
12851276
}
12861277
}
12871278

0 commit comments

Comments
 (0)