Skip to content

Timeout stops threads if they are uninterruptible. #811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions core/src/main/java/cucumber/runtime/Timeout.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cucumber.runtime;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -16,21 +16,35 @@ public static <T> T timeout(Callback<T> callback, long timeoutMillis) throws Thr
final AtomicBoolean done = new AtomicBoolean();

ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
ScheduledFuture<?> timer = executorService.schedule(new Runnable() {
ScheduledFuture<?> interruptTimer = executorService.schedule(new Runnable() {
@Override
public void run() {
if (!done.get()) {
executionThread.interrupt();
}
}
}, timeoutMillis, TimeUnit.MILLISECONDS);

// We'll start a second timer that will stop() the thread in case interrupt()
// doesn't do it. This can happen for *uninterruptible threads*.
ScheduledFuture<?> stopTimer = executorService.schedule(new Runnable() {
@Override
public void run() {
if (!done.get()) {
executionThread.stop();
}
}
}, timeoutMillis * 2, TimeUnit.MILLISECONDS);
try {
return callback.call();
} catch (InterruptedException timeout) {
throw new TimeoutException("Timed out after " + timeoutMillis + "ms.");
} catch (ThreadDeath threadDeath) {
throw new TimeoutException("Timed out after " + timeoutMillis + "ms. (Stopped the thread was uninterruptible).");
} finally {
done.set(true);
timer.cancel(true);
interruptTimer.cancel(true);
stopTimer.cancel(true);
executorService.shutdownNow();
}

Expand Down
80 changes: 48 additions & 32 deletions core/src/test/java/cucumber/runtime/TimeoutTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cucumber.runtime;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeoutException;
Expand All @@ -11,6 +13,9 @@
import static org.junit.Assert.fail;

public class TimeoutTest {
@Rule
public ExpectedException exception = ExpectedException.none();

@Test
public void doesnt_time_out_if_it_doesnt_take_too_long() throws Throwable {
final Slow slow = new Slow();
Expand All @@ -23,42 +28,54 @@ public String call() throws Throwable {
assertEquals("slept 10ms", what);
}

@Test(expected = TimeoutException.class)
@Test
public void times_out_if_it_takes_too_long() throws Throwable {
final Slow slow = new Slow();
Timeout.timeout(new Timeout.Callback<String>() {
@Override
public String call() throws Throwable {
return slow.slow(100);
}
}, 50);
fail();
try {
final Slow slow = new Slow();
Timeout.timeout(new Timeout.Callback<String>() {
@Override
public String call() throws Throwable {
return slow.slow(100);
}
}, 50);
fail();
} catch (TimeoutException expected) {
assertEquals("Timed out after 50ms.", expected.getMessage());
}
}

@Test(expected = TimeoutException.class)
public void times_out_infinite_loop_if_it_takes_too_long() throws Throwable {
final Slow slow = new Slow();
Timeout.timeout(new Timeout.Callback<Void>() {
@Override
public Void call() throws Throwable {
slow.infinite();
return null;
}
}, 10);
fail();
@Test
public void times_out_infinite_spin_loop_if_it_takes_too_long() throws Throwable {
try {
final Slow slow = new Slow();
Timeout.timeout(new Timeout.Callback<Void>() {
@Override
public Void call() throws Throwable {
slow.infiniteSpin();
return null;
}
}, 10);
fail();
} catch (TimeoutException expected) {
assertEquals("Timed out after 10ms. (Stopped the thread was uninterruptible).", expected.getMessage());
}
}

@Test(expected = TimeoutException.class)
@Test
public void times_out_infinite_latch_wait_if_it_takes_too_long() throws Throwable {
final Slow slow = new Slow();
Timeout.timeout(new Timeout.Callback<Void>() {
@Override
public Void call() throws Throwable {
slow.infiniteLatchWait();
return null;
}
}, 10);
fail();
try {
final Slow slow = new Slow();
Timeout.timeout(new Timeout.Callback<Void>() {
@Override
public Void call() throws Throwable {
slow.infiniteLatchWait();
return null;
}
}, 10);
fail();
} catch (TimeoutException expected) {
assertEquals("Timed out after 10ms.", expected.getMessage());
}
}

@Test
Expand Down Expand Up @@ -93,9 +110,8 @@ public String slow(int millis) throws InterruptedException {
return String.format("slept %sms", millis);
}

public void infinite() throws InterruptedException {
public void infiniteSpin() throws InterruptedException {
while (true) {
sleep(1);
}
}

Expand Down