From e0c9b2b7b40743ec4f5f1dd5bc1eaf0e815ed72e Mon Sep 17 00:00:00 2001 From: Peter Ballard Date: Mon, 20 Jul 2015 14:44:28 +0100 Subject: Fixed CancelledKeyException in JavaConnection If synchroniseReads was enabled then if was possible for CancelledKeyExceptions to occur if the channel closed, whilst data was being procsessed on the event loop. I have also made the selectorKey_ reference volatile as this is used by both threads since my previous change 1afab8c1f2c417b08371bc12569e03171851d785 Change-Id: I48edcab9ef090b8a4561331d6aecc7f95cb8924d diff --git a/src/com/isode/stroke/network/JavaConnection.java b/src/com/isode/stroke/network/JavaConnection.java index 66f068f..db748e8 100644 --- a/src/com/isode/stroke/network/JavaConnection.java +++ b/src/com/isode/stroke/network/JavaConnection.java @@ -49,7 +49,7 @@ public class JavaConnection extends Connection implements EventOwner { private final HostAddressPort address_; private final List writeBuffer_ = Collections.synchronizedList(new ArrayList()); - private SelectionKey selectionKey_; + private volatile SelectionKey selectionKey_; private boolean disconnected_ = false; public Worker(HostAddressPort address) { @@ -311,7 +311,9 @@ public class JavaConnection extends Connection implements EventOwner { // https://code.google.com/p/android/issues/detail?id=80785 if (synchroniseReads_ && selector_ != null) { synchronized (selectorLock_) { - selectionKey_.interestOps(SelectionKey.OP_READ); + if (selectionKey_.isValid()) { + selectionKey_.interestOps(SelectionKey.OP_READ); + } if (selector_.isOpen()) { selector_.wakeup(); } -- cgit v0.10.2-6-g49f6