Skip to content

Commit 60b83de

Browse files
daschlMichael Nitschinger
authored andcommitted
SPY-160: Avoid ConcurrentModificationException with the retry queue
Motivation ---------- ConcurrentModificationException will show up since the code can recursively modify itself. Modifications ------------- Copy the original list into a temporary new one to avoid the exception. Result ------ Redistribute on the retry list will work as expected, without an exception. Change-Id: Ieff1a87136bef38280b7f139192419073d6a878a Reviewed-on: http://review.couchbase.org/34977 Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com> Reviewed-by: Matt Ingenthron <matt@couchbase.com>
1 parent 7a8f15e commit 60b83de

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.text.MessageFormat;
3939
import java.util.ArrayList;
4040
import java.util.Collection;
41+
import java.util.Collections;
4142
import java.util.ConcurrentModificationException;
4243
import java.util.HashSet;
4344
import java.util.IdentityHashMap;
@@ -196,7 +197,7 @@ public class MemcachedConnection extends SpyThread {
196197
/**
197198
* Holds operations that need to be retried.
198199
*/
199-
private final Collection<Operation> retryOps;
200+
private final List<Operation> retryOps;
200201

201202
/**
202203
* Holds all nodes that are scheduled for shutdown.
@@ -247,7 +248,7 @@ public MemcachedConnection(final int bufSize, final ConnectionFactory f,
247248
opFact = opfactory;
248249
timeoutExceptionThreshold = f.getTimeoutExceptionThreshold();
249250
selector = Selector.open();
250-
retryOps = new ArrayList<Operation>();
251+
retryOps = Collections.synchronizedList(new ArrayList<Operation>());
251252
nodesToShutdown = new ConcurrentLinkedQueue<MemcachedNode>();
252253
listenerExecutorService = f.getListenerExecutorService();
253254
this.bufSize = bufSize;
@@ -429,8 +430,11 @@ private void handleOperationalTasks() throws IOException {
429430
if (!shutDown && !reconnectQueue.isEmpty()) {
430431
attemptReconnects();
431432
}
432-
redistributeOperations(retryOps);
433-
retryOps.clear();
433+
434+
if (!retryOps.isEmpty()) {
435+
redistributeOperations(new ArrayList<Operation>(retryOps));
436+
retryOps.clear();
437+
}
434438

435439
handleShutdownQueue();
436440
}

0 commit comments

Comments
 (0)