Skip to content

Commit

Permalink
SPY-192: Ensure connection key is valid before checking if writable.
Browse files Browse the repository at this point in the history
Motivation
----------
It has been discovered in the wild that sometimes CancelledKeyException are
received even if the key has been checked for validity before. It turns out
that the validity can change between the check and the next function call.

Modifications
-------------
The changeset makes sure to check if it is valid on both reading and
subsequent writing, reducing the chance of race conditions and interleaved
state changes.

Result
------
More stable behavior at runtime and less chance of CancelledKeyException.

Change-Id: Ic0db65f9c7ccc3a7b9738aaff0e3c7e60d7f25c2
Reviewed-on: http://review.couchbase.org/72270
Reviewed-by: Michael Nitschinger <michael@nitschinger.at>
Tested-by: Michael Nitschinger <michael@nitschinger.at>
  • Loading branch information
ggrochow authored and daschl committed Feb 1, 2017
1 parent fd7e071 commit 2b6e3bb
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/main/java/net/spy/memcached/MemcachedConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -713,18 +713,15 @@ private void handleIO(final SelectionKey sk) {
* @param node th enode to read write from.
* @throws IOException if an error occurs during read/write.
*/
private void handleReadsAndWrites(final SelectionKey sk,
final MemcachedNode node) throws IOException {
if (sk.isValid()) {
if (sk.isReadable()) {
handleReads(node);
private void handleReadsAndWrites(final SelectionKey sk, final MemcachedNode node) throws IOException {
if (sk.isValid() && sk.isReadable()) {
handleReads(node);
}
if (sk.isWritable()) {
handleWrites(node);

if (sk.isValid() && sk.isWritable()) {
handleWrites(node);
}
}
}

/**
* Finish the connect phase and potentially verify its liveness.
*
Expand Down

0 comments on commit 2b6e3bb

Please sign in to comment.