Skip to content

Commit

Permalink
SPY-171: More robust shutdown handling characteristics.
Browse files Browse the repository at this point in the history
Motivation
----------
It has been reported several times that the IO thread kept lingering
around even after a shutdown() call.

Modifications
-------------
Since its run() method is kept alive by the "running" variable, the
code now makes sure to always set it to false, even if an exception
occurs during the connection shutdown process.

Also, a slightly misleading IOException has been removed in favor of
just silently moving on if shutdown is in progress.

Result
------
More stable and predictable shutdown behavior.

Change-Id: I99f3effbbb20a78a2705ee7f3f839e9753fb2a4a
Reviewed-on: http://review.couchbase.org/37724
Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
  • Loading branch information
daschl authored and Michael Nitschinger committed Jun 4, 2014
1 parent 4aae4c5 commit 1a92696
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/main/java/net/spy/memcached/MemcachedConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ private boolean selectorsMakeSense() {
*/
public void handleIO() throws IOException {
if (shutDown) {
throw new IOException("No IO while shut down");
getLogger().debug("No IO while shut down.");
return;
}

handleInputQueue();
Expand Down Expand Up @@ -1285,23 +1286,26 @@ public CountDownLatch broadcastOperation(final BroadcastOpFactory of,
*/
public void shutdown() throws IOException {
shutDown = true;

Selector s = selector.wakeup();
assert s == selector : "Wakeup returned the wrong selector.";
for (MemcachedNode node : locator.getAll()) {
if (node.getChannel() != null) {
node.getChannel().close();
node.setSk(null);
if (node.getBytesRemainingToWrite() > 0) {
getLogger().warn("Shut down with %d bytes remaining to write",
try {
Selector s = selector.wakeup();
assert s == selector : "Wakeup returned the wrong selector.";
for (MemcachedNode node : locator.getAll()) {
if (node.getChannel() != null) {
node.getChannel().close();
node.setSk(null);
if (node.getBytesRemainingToWrite() > 0) {
getLogger().warn("Shut down with %d bytes remaining to write",
node.getBytesRemainingToWrite());
}
getLogger().debug("Shut down channel %s", node.getChannel());
}
getLogger().debug("Shut down channel %s", node.getChannel());
}

selector.close();
getLogger().debug("Shut down selector %s", selector);
} finally {
running = false;
}
running = false;
selector.close();
getLogger().debug("Shut down selector %s", selector);
}

@Override
Expand Down

0 comments on commit 1a92696

Please sign in to comment.