Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Add wait for exit function in WakeableLooper and fix exiting code in SocketChannelHelper #2651

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

nwangtw
Copy link
Contributor

@nwangtw nwangtw commented Jan 2, 2018

Add waitForExit() function in WakeableLooper and use it to avoid racing condition in SocketChannelHelper's forceFlushWithBestEffort() function.

The main motivation of this change is the flaky unit tests in HeronServerTest. However it is possible to happen in real topologies as well in the exiting process.

In the test, a write task is scheduled and executed in a child thread and the main thread calls the flush function. Both threads are consuming the same buffer. When the main thread calls writeToChannel() when buffer size is 0, an assert(and exception) would be triggered.
public class OutgoingPacket {
...
public int writeToChannel(SocketChannel channel) {
int remaining = buffer.remaining();
assert remaining > 0;
....

@@ -213,6 +214,10 @@ public void write() {
// Force to flush all data in underneath buffer queue to socket with best effort
// It is most likely happen when we are handling some unexpected cases, such as exiting
public void forceFlushWithBestEffort() {
looper.exitLoop();
// Wait for NIO loop to confirm stopping process.
looper.waitForExit(10, TimeUnit.SECONDS);
Copy link
Contributor

@huijunw huijunw Jan 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if the looper does not stop after 10 seconds

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function stops waiting and returns false.

In this case, the code works pretty much the same way as before after the waiting. Except exitLoop() set a flag to true so the looper won't do any real work afterwards.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants