diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/BasicProcessWithCallBack.java b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/BasicProcessWithCallBack.java index 100e6168a..3f4925e04 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/BasicProcessWithCallBack.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/BasicProcessWithCallBack.java @@ -123,7 +123,7 @@ public void run() { if (shimmerMSG.mB instanceof EndThread){ stop=true; } else { - processMsgFromCallback(shimmerMSG); + processMsgSafely(shimmerMSG); } } catch (InterruptedException e) { // TODO Auto-generated catch block @@ -134,8 +134,22 @@ public void run() { //as a safety precaution, remove all its callbacks removeSetWaitForDataAll(); - + }; + + /** One bad message must not kill the consumer. If this thread died, + * mQueue would stop draining, every producer would block in put() once + * it held 1024 messages, and anything that then sends a callback - + * including an application's shutdown hook - would hang for good. */ + private void processMsgSafely(ShimmerMsg shimmerMSG) { + try { + processMsgFromCallback(shimmerMSG); + } catch (RuntimeException e) { + System.err.println(getName() + ": exception processing callback message " + + shimmerMSG.mIdentifier + "; the consumer thread continues"); + e.printStackTrace(); + } + } } /** This allows the class to receive msgs from the class passed in as an argument diff --git a/ShimmerDriver/src/test/java/com/shimmerresearch/driver/BasicProcessWithCallBackTest.java b/ShimmerDriver/src/test/java/com/shimmerresearch/driver/BasicProcessWithCallBackTest.java new file mode 100644 index 000000000..e658cc557 --- /dev/null +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/driver/BasicProcessWithCallBackTest.java @@ -0,0 +1,54 @@ +package com.shimmerresearch.driver; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.junit.Test; + +public class BasicProcessWithCallBackTest { + + /** Records each message it gets, and throws for identifier 1. */ + private static class ThrowingConsumer extends BasicProcessWithCallBack { + final List received = new CopyOnWriteArrayList(); + final CountDownLatch gotLast = new CountDownLatch(1); + + @Override + protected void processMsgFromCallback(ShimmerMsg shimmerMSG) { + received.add(shimmerMSG.mIdentifier); + if (shimmerMSG.mIdentifier == 1) { + throw new IllegalStateException("simulated failure in a callback handler"); + } + if (shimmerMSG.mIdentifier == 3) { + gotLast.countDown(); + } + } + } + + /** + * A runtime exception from one message must not kill the consumer thread. + * When it did, the queue stopped draining, producers blocked in put() once it + * held 1024 messages, and Consensys hung at exit inside its shutdown hook. + */ + @Test + public void consumerThreadSurvivesExceptionInHandler() throws Exception { + ThrowingConsumer consumer = new ThrowingConsumer(); + consumer.startConsumerThreadIfNull(); + try { + consumer.queueMethod(new ShimmerMsg(1, null)); + consumer.queueMethod(new ShimmerMsg(2, null)); + consumer.queueMethod(new ShimmerMsg(3, null)); + + assertTrue("message after the exception was never processed", + consumer.gotLast.await(5, TimeUnit.SECONDS)); + assertEquals(3, consumer.received.size()); + assertTrue(consumer.mGUIConsumerThread.isAlive()); + } finally { + consumer.stopConsumerThread(); + } + } +}