Skip to content

Commit 2f3390e

Browse files
[GR-72496] Add uncaught exception handler to threads started by Stressor.
PullRequest: graal/23285
2 parents 43a3b52 + de88a9a commit 2f3390e

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jfr/Stressor.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@
2626

2727
package com.oracle.svm.test.jfr;
2828

29+
import java.util.concurrent.atomic.AtomicReference;
30+
2931
/**
3032
* Class to help run multiple threads executing some task.
3133
*/
3234
public class Stressor {
33-
public static void execute(int numberOfThreads, Runnable task) {
35+
public static void execute(int numberOfThreads, Runnable task) throws Throwable {
3436
Thread[] threads = new Thread[numberOfThreads];
37+
StoreUncaughtException[] uncaughtExceptions = new StoreUncaughtException[numberOfThreads];
38+
3539
for (int i = 0; i < numberOfThreads; i++) {
3640
threads[i] = new Thread(task);
41+
uncaughtExceptions[i] = new StoreUncaughtException();
42+
threads[i].setUncaughtExceptionHandler(uncaughtExceptions[i]);
3743
}
3844

3945
for (int i = 0; i < numberOfThreads; i++) {
@@ -43,9 +49,28 @@ public static void execute(int numberOfThreads, Runnable task) {
4349
for (int i = 0; i < numberOfThreads; i++) {
4450
try {
4551
threads[i].join();
52+
53+
/* Rethrow any uncaught exceptions. */
54+
Throwable throwable = uncaughtExceptions[i].get();
55+
if (throwable != null) {
56+
throw throwable;
57+
}
4658
} catch (InterruptedException e) {
4759
throw new RuntimeException(e);
4860
}
4961
}
5062
}
63+
64+
private static final class StoreUncaughtException implements Thread.UncaughtExceptionHandler {
65+
private final AtomicReference<Throwable> throwable = new AtomicReference<>();
66+
67+
public Throwable get() {
68+
return throwable.get();
69+
}
70+
71+
@Override
72+
public void uncaughtException(Thread t, Throwable e) {
73+
this.throwable.set(e);
74+
}
75+
}
5176
}

substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jfr/TestJfrStreamingBasic.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ public void test() throws Throwable {
7777
stream.onFlush(() -> {
7878
if (firstFlush) {
7979
firstFlush = false;
80-
Stressor.execute(THREADS, eventEmitter);
80+
81+
try {
82+
Stressor.execute(THREADS, eventEmitter);
83+
} catch (Throwable throwable) {
84+
throw new RuntimeException(throwable);
85+
}
8186
}
8287
});
8388

substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jfr/TestJfrStreamingCount.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ public void test() throws Throwable {
8686
Stressor.execute(THREADS, eventEmitter);
8787

8888
waitUntilTrue(() -> emittedEventsPerType.get() >= EXPECTED_EVENTS_PER_TYPE);
89-
waitUntilTrue(() -> classEvents.get() >= EXPECTED_EVENTS_PER_TYPE);
9089
waitUntilTrue(() -> integerEvents.get() >= EXPECTED_EVENTS_PER_TYPE);
9190
waitUntilTrue(() -> stringEvents.get() >= EXPECTED_EVENTS_PER_TYPE);
91+
waitUntilTrue(() -> classEvents.get() >= EXPECTED_EVENTS_PER_TYPE);
9292

9393
assertEquals(EXPECTED_EVENTS_PER_TYPE, emittedEventsPerType.get());
94-
assertEquals(EXPECTED_EVENTS_PER_TYPE, classEvents.get());
9594
assertEquals(EXPECTED_EVENTS_PER_TYPE, integerEvents.get());
9695
assertEquals(EXPECTED_EVENTS_PER_TYPE, stringEvents.get());
96+
assertEquals(EXPECTED_EVENTS_PER_TYPE, classEvents.get());
9797

9898
stopStream(stream, TestJfrStreamingCount::validateEvents);
9999
}

0 commit comments

Comments
 (0)