Skip to content

Commit 6b30b45

Browse files
committed
Improve os._exit
1 parent 14a6944 commit 6b30b45

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.math.BigInteger;
4444
import java.security.SecureRandom;
4545
import java.util.ArrayList;
46+
import java.util.Arrays;
4647
import java.util.Collections;
4748
import java.util.List;
4849
import java.util.Map;
@@ -133,6 +134,7 @@
133134
import com.oracle.graal.python.util.OverflowException;
134135
import com.oracle.truffle.api.CompilerDirectives;
135136
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
137+
import com.oracle.truffle.api.ThreadLocalAction;
136138
import com.oracle.truffle.api.dsl.Bind;
137139
import com.oracle.truffle.api.dsl.Cached;
138140
import com.oracle.truffle.api.dsl.Cached.Exclusive;
@@ -2571,8 +2573,30 @@ public abstract static class ExitNode extends PythonUnaryBuiltinNode {
25712573
@TruffleBoundary
25722574
@Specialization
25732575
Object exit(int status) {
2574-
// TODO: use a safepoint action to throw this exception to all running threads
2575-
throw new PythonExitException(this, status);
2576+
PythonContext context = getContext();
2577+
if (context.getOption(PythonOptions.RunViaLauncher)) {
2578+
Runtime.getRuntime().halt(status);
2579+
}
2580+
List<Thread> otherThreads = new ArrayList<>(Arrays.asList(context.getThreads()));
2581+
otherThreads.remove(context.getMainThread());
2582+
otherThreads.remove(Thread.currentThread());
2583+
context.getEnv().submitThreadLocal(otherThreads.toArray(new Thread[0]), new ThreadLocalAction(true, false) {
2584+
@Override
2585+
protected void perform(Access access) {
2586+
throw new ThreadDeath();
2587+
}
2588+
});
2589+
if (Thread.currentThread() == context.getMainThread()) {
2590+
throw new PythonExitException(this, status);
2591+
} else {
2592+
context.getEnv().submitThreadLocal(new Thread[]{context.getMainThread()}, new ThreadLocalAction(true, false) {
2593+
@Override
2594+
protected void perform(Access access) {
2595+
throw new PythonExitException(ExitNode.this, status);
2596+
}
2597+
});
2598+
}
2599+
throw new ThreadDeath();
25762600
}
25772601
}
25782602

0 commit comments

Comments
 (0)