Skip to content

Commit 39f4598

Browse files
committed
[GR-17457] Fix Context#close(true) test and always propagate ThreadDeath
PullRequest: truffleruby/2384
2 parents 15ab61b + 4bca34f commit 39f4598

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

src/main/java/org/truffleruby/core/kernel/AtExitManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private RubyException runExitHooks(Deque<RubyProc> stack, String name) {
6868
} catch (RaiseException e) {
6969
handleAtExitException(context, e.getException());
7070
lastException = e.getException();
71-
} catch (ExitException e) {
71+
} catch (ExitException | ThreadDeath e) {
7272
throw e;
7373
} catch (RuntimeException | Error e) {
7474
BacktraceFormatter.printInternalError(context, e, "Unexpected internal exception in " + name);

src/main/java/org/truffleruby/core/thread/ThreadManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ private void threadMain(RubyThread thread, Node currentNode, Supplier<Object> ta
306306
// Handlers in the same order as in FiberManager
307307
} catch (KillException e) {
308308
setThreadValue(thread, Nil.INSTANCE);
309+
} catch (ThreadDeath e) { // Context#close(true)
310+
throw e;
309311
} catch (RaiseException e) {
310312
setException(thread, e.getException(), currentNode);
311313
} catch (DynamicReturnException e) {

src/main/java/org/truffleruby/language/exceptions/TopLevelRaiseHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public int execute(Runnable body) {
4040
} catch (ExitException e) {
4141
// hard #exit!, return immediately, skip at_exit hooks
4242
return e.getCode();
43+
} catch (ThreadDeath e) { // Context#close(true)
44+
throw e;
4345
} catch (RuntimeException | Error e) {
4446
BacktraceFormatter.printInternalError(
4547
getContext(),
@@ -66,6 +68,8 @@ public int execute(Runnable body) {
6668
} catch (ExitException e) {
6769
// hard #exit! during at_exit: ignore the main script exception
6870
exitCode = e.getCode();
71+
} catch (ThreadDeath e) { // Context#close(true)
72+
throw e;
6973
} catch (RuntimeException | Error e) { // Internal error
7074
BacktraceFormatter.printInternalError(
7175
getContext(),

src/test/java/org/truffleruby/MiscTest.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
import static org.junit.Assert.assertEquals;
1313
import static org.junit.Assert.assertNotEquals;
1414
import static org.junit.Assert.assertTrue;
15-
16-
import java.util.Timer;
17-
import java.util.TimerTask;
15+
import static org.junit.Assert.fail;
1816

1917
import org.graalvm.polyglot.Context;
2018
import org.graalvm.polyglot.PolyglotException;
@@ -46,28 +44,31 @@ public void testMembersAndStringUnboxing() {
4644
}
4745

4846
@Test
49-
public void timeoutExecution() {
47+
public void timeoutExecution() throws Throwable {
5048
Context context = RubyTest.createContext();
5149

52-
Timer timer = new Timer();
5350
// schedule a timeout in 1s
54-
timer.schedule(new TimerTask() {
55-
@Override
56-
public void run() {
57-
try {
58-
context.close(true);
59-
} catch (PolyglotException e) {
60-
assertTrue(e.isCancelled());
61-
}
51+
TestingThread thread = new TestingThread(() -> {
52+
try {
53+
Thread.sleep(1000);
54+
context.close(true);
55+
} catch (InterruptedException e) {
56+
e.printStackTrace();
57+
fail();
58+
} catch (PolyglotException e) {
59+
assertTrue(e.isCancelled());
6260
}
63-
}, 1000);
61+
});
6462

63+
thread.start();
6564
try {
6665
String maliciousCode = "while true; end";
6766
context.eval("ruby", maliciousCode);
6867
Assert.fail();
6968
} catch (PolyglotException e) {
7069
assertTrue(e.isCancelled());
70+
} finally {
71+
thread.join();
7172
}
7273
}
7374

0 commit comments

Comments
 (0)