Skip to content

Commit fde7a94

Browse files
daschlMichael Nitschinger
authored andcommitted
SPY-165: Do not cache the selectedKeys.
Motivation ---------- Caching the selected keys may cause issues if the selector is accessed by different threads, also the loop for the keys is not using the iterator correct. Modifications ------------- Make it not cache the keys and also correctly make use of an iterator to loop through the keys. Result ------ No stale/cached selected keys and correctly using the iterator should lead to better stability during failure cases. Thanks to Brad Svee (@sveesible) for suggesting the fix Change-Id: If82cdfc810c758a4196415cb709a03ebd72c3d15 Reviewed-on: http://review.couchbase.org/36222 Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com> Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
1 parent 206b198 commit fde7a94

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/main/java/net/spy/memcached/MemcachedConnection.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
import java.util.Iterator;
6767
import java.util.List;
6868
import java.util.Map;
69-
import java.util.Set;
7069
import java.util.SortedMap;
7170
import java.util.TreeMap;
7271
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -405,19 +404,21 @@ public void handleIO() throws IOException {
405404
getLogger().debug("Selecting with delay of %sms", delay);
406405
assert selectorsMakeSense() : "Selectors don't make sense.";
407406
int selected = selector.select(delay);
408-
Set<SelectionKey> selectedKeys = selector.selectedKeys();
407+
//Set<SelectionKey> selectedKeys = selector.selectedKeys();
409408

410-
if (selectedKeys.isEmpty() && !shutDown) {
409+
if (selector.selectedKeys().isEmpty() && !shutDown) {
411410
handleEmptySelects();
412411
} else {
413412
getLogger().debug("Selected %d, selected %d keys", selected,
414-
selectedKeys.size());
413+
selector.selectedKeys().size());
415414
emptySelects = 0;
416415

417-
for (SelectionKey sk : selectedKeys) {
416+
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
417+
while(iterator.hasNext()) {
418+
SelectionKey sk = iterator.next();
418419
handleIO(sk);
420+
iterator.remove();
419421
}
420-
selectedKeys.clear();
421422
}
422423

423424
handleOperationalTasks();

0 commit comments

Comments
 (0)