diff --git a/hystrix-core/src/main/java/com/netflix/hystrix/util/HystrixTimer.java b/hystrix-core/src/main/java/com/netflix/hystrix/util/HystrixTimer.java index 3c10e6276..a9c92bd7b 100644 --- a/hystrix-core/src/main/java/com/netflix/hystrix/util/HystrixTimer.java +++ b/hystrix-core/src/main/java/com/netflix/hystrix/util/HystrixTimer.java @@ -119,6 +119,7 @@ private static class TimerReference extends SoftReference { @Override public void clear() { super.clear(); + INSTANCE.executor.get().getThreadPool().remove((Runnable) f); // stop this ScheduledFuture from any further executions f.cancel(false); } diff --git a/hystrix-core/src/test/java/com/netflix/hystrix/util/HystrixTimerTest.java b/hystrix-core/src/test/java/com/netflix/hystrix/util/HystrixTimerTest.java index 8d873cf3e..879674ba1 100644 --- a/hystrix-core/src/test/java/com/netflix/hystrix/util/HystrixTimerTest.java +++ b/hystrix-core/src/test/java/com/netflix/hystrix/util/HystrixTimerTest.java @@ -207,6 +207,22 @@ public HystrixTimerThreadPoolProperties getTimerThreadPoolProperties() { } + @Test + public void testCancelledTasksInQueueGetRemoved() { + HystrixTimer timer = HystrixTimer.getInstance(); + TestListener l1 = new TestListener(1000, "A"); + Reference l1ref = timer.addTimerListener(l1); + + assertTrue(timer.executor.get().getThreadPool().getQueue().size() > 0); + + l1ref.clear(); + + System.out.println("l1 ticks: " + l1.tickCount.get()); + assertTrue(l1.tickCount.get() == 0); + + assertTrue(timer.executor.get().getThreadPool().getQueue().size() == 0); + } + private static class TestListener implements TimerListener { private final int interval;