Skip to content

Commit

Permalink
Fix: Improve exceptions produced by ThreadingDetector crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
malte0811 committed Feb 22, 2022
1 parent 78795b8 commit ba7038e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.BooleanSupplier;

@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
public class SmallThreadingDetector {
Expand Down Expand Up @@ -73,12 +74,12 @@ private static void startCrash(SmallThreadDetectable owner, String name) {

private static void crashAcquire(SmallThreadDetectable owner) {
var state = getAndWait(owner, ThreadRole.ACQUIRE);
throw ThreadingDetector.makeThreadingException(state.name, state.acquireThread);
throw state.mainException;
}

private static void crashRelease(SmallThreadDetectable owner) {
var state = getAndWait(owner, ThreadRole.RELEASE);
throw ThreadingDetector.makeThreadingException(state.name, state.releaseThread);
throw state.mainException;
}

private static void crashBystander(SmallThreadDetectable owner) {
Expand Down Expand Up @@ -110,6 +111,7 @@ private static class CrashingState {
final String name;
Thread acquireThread;
Thread releaseThread;
RuntimeException mainException;

private CrashingState(String name) {
this.name = name;
Expand All @@ -126,24 +128,34 @@ public synchronized void waitUntilReady(ThreadRole role) {
// Notify other threads waiting for this crash to be ready
notifyAll();
try {
final long maxTotalTime = 10000;
final var start = System.currentTimeMillis();
while (acquireThread == null || releaseThread == null) {
if (System.currentTimeMillis() - start > maxTotalTime) {
// Crash without both threads present if we don't manage to "find" them within 10 seconds
// Happens e.g. when a release call is just missing, vanilla would hang indefinitely instead
// in this case
throw new RuntimeException(
"Threading detector crash did not find other thread, missing release call?"
);
}
// Release lock on this for up to 10 seconds, or until the other threads are ready
this.wait(maxTotalTime);
waitUntilOrCrash(() -> acquireThread != null && releaseThread != null);
if (role == ThreadRole.ACQUIRE) {
mainException = ThreadingDetector.makeThreadingException(name, releaseThread);
notifyAll();
} else {
waitUntilOrCrash(() -> mainException != null);
}
} catch (InterruptedException x) {
Thread.currentThread().interrupt();
}
}

private synchronized void waitUntilOrCrash(BooleanSupplier isReady) throws InterruptedException {
final long maxTotalTime = 10000;
final var start = System.currentTimeMillis();
while (!isReady.getAsBoolean()) {
if (System.currentTimeMillis() - start > maxTotalTime) {
// Crash without both threads present if we don't manage to "find" them within 10 seconds
// Happens e.g. when a release call is just missing, vanilla would hang indefinitely instead
// in this case
throw new RuntimeException(
"Threading detector crash did not find other thread, missing release call?"
);
}
// Release lock on this for up to 10 seconds, or until the other threads are ready
this.wait(maxTotalTime);
}
}
}

private enum ThreadRole {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
Expand Down Expand Up @@ -49,15 +51,18 @@ public void testHandoff() throws InterruptedException {
public void testRace() throws InterruptedException {
var obj = new OwnedObject();
AtomicBoolean anyTripped = new AtomicBoolean(false);
List<Thread> threads = new ArrayList<>(10);
for (int i = 0; i < 10; ++i)
runOnNewThread(() -> {
threads.add(runOnNewThread(() -> {
final long start = System.currentTimeMillis();
while (!anyTripped.get() && System.currentTimeMillis() - start < 1000) {
SmallThreadingDetector.acquire(obj, "test");
SmallThreadingDetector.release(obj);
}
}, $ -> anyTripped.set(true));
}, $ -> anyTripped.set(true)));
Thread.sleep(1000);
for (var thread : threads)
thread.join();
Assertions.assertTrue(anyTripped.get());
}

Expand Down

0 comments on commit ba7038e

Please sign in to comment.