Skip to content

Commit

Permalink
Fix a bug introduced by 7970635 which can cause thread to spin in an …
Browse files Browse the repository at this point in the history
…infinite loop. (netty#9579)

Motivation:
peek() is implemented in a similar way to poll() for the mpsc queue, thus it is more like a consumer call.
It is possible that we could have multiple thread call peek() and possibly one thread calls poll() at at the same time.
This lead to multiple consumer scenario, which violates the multiple producer single consumer condition and could lead to spin in an infinite loop in peek()

Modification:
Use isEmpty() instead of peek() to check if task queue is empty

Result:
Dont violate the mpsc semantics.
  • Loading branch information
wyzhang authored and normanmaurer committed Sep 19, 2019
1 parent 3ad0374 commit 338e1a9
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public void run() {
//
// The above cases can be distinguished by performing a
// compareAndSet(NONE, RUNNING). If it returns "false", it is case 1; otherwise it is case 2.
if (tasks.peek() == null || !state.compareAndSet(NONE, RUNNING)) {
if (tasks.isEmpty() || !state.compareAndSet(NONE, RUNNING)) {
return; // done
}
}
Expand Down

0 comments on commit 338e1a9

Please sign in to comment.