How does one wait for all event handlers to fully complete processing after shutdown?
See original GitHub issueVersion: 3.3.2
It appears that event handlers are not necessarily finished executing when Disruptor.shutdown()
returns. What is the definitive way to detect when all event handlers have finished execution after the disruptor is shutdown?
As a point of illustration, this unit test sometimes prints 0, and sometimes prints 1:
public class ShutdownTest {
volatile int value = 0;
@Test
public void shutdownFailure() throws TimeoutException
{
Disruptor<Void> disruptor = new Disruptor<>(() -> null, 8, (ThreadFactory) Thread::new);
disruptor.handleEventsWith(this::updateValue);
disruptor.start();
disruptor.getRingBuffer().publishEvent((m, s) -> {});
disruptor.shutdown(10, TimeUnit.SECONDS);
System.out.println(value); // Sometimes prints 0, but if updateValue has completed it would be expected to always be 1.
}
private void updateValue(Message message, long sequence, boolean endOfBatch)
{
value = 1;
}
}
What is the correct way of ensuring that “1” is printed, always?
Issue Analytics
- State:
- Created 7 years ago
- Comments:9
Top Results From Across the Web
How to wait for all threads to finish, using ExecutorService?
Basically on an ExecutorService you call shutdown() and then awaitTermination() : ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(.
Read more >Handling Events :: Eloquent JavaScript
For most types of events, the JavaScript event handlers are called before the default behavior takes place. If the handler doesn't want this...
Read more >Process.WaitForExit Method (System.Diagnostics)
This method instructs the Process component to wait an infinite amount of time for the process and event handlers to exit. This can...
Read more >Window: beforeunload event - Web APIs | MDN
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible...
Read more >Page: DOMContentLoaded, load, beforeunload, unload
When a visitor leaves the page, the unload event triggers on window . We can do something there that doesn't involve a delay,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It is a source of confusion that I want to fix, but doing it transparently (without API or behaviour change) is tricky and given that it tends to only turn up in unit tests and there is a fairly straight-forward workaround it keeps falling to the bottom of the priority list.
That’s the primary source of confusion for me, I suppose. In this example, the thread does seem to actually start and handle traffic, but because the shutdown happens so quickly it seems to skip doing the onShutdown method.
The logs from a failed iteration of this test are: Event 1 Start 2 Event 2
That indicates to me that the threads have already started because:
Despite these two functions being executed, the onShutdown() is never called when the thread is killed. That’s pretty confusing as I would expect after an onStart() you would always see an onShutdown().
This probably won’t ever be an issue in production code because it’s unlikely people would ever start and stop the disruptor so quickly, but it’s a bit of a nuisance in unit testing where you’re more likely to be starting and stopping them in quick succession. As you’ve pointed out, it can be worked around by adding the startup latch and ensuring the startup has finished before the shutdown is initiated.