Author: markt
Date: Mon Jul  1 10:57:50 2013
New Revision: 1498363

URL: http://svn.apache.org/r1498363
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=54086
If stopListening is called while the thread is processing selector events it is 
possible for two threads to try to use the keys collection at the same time. 
Prevent the ConcurrentModificationException that can occur.

Modified:
    
tomcat/trunk/java/org/apache/catalina/tribes/transport/LocalStrings.properties
    tomcat/trunk/java/org/apache/catalina/tribes/transport/nio/NioReceiver.java

Modified: 
tomcat/trunk/java/org/apache/catalina/tribes/transport/LocalStrings.properties
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/tribes/transport/LocalStrings.properties?rev=1498363&r1=1498362&r2=1498363&view=diff
==============================================================================
--- 
tomcat/trunk/java/org/apache/catalina/tribes/transport/LocalStrings.properties 
(original)
+++ 
tomcat/trunk/java/org/apache/catalina/tribes/transport/LocalStrings.properties 
Mon Jul  1 10:57:50 2013
@@ -29,6 +29,7 @@ NioReceiver.requestError=Unable to proce
 NioReceiver.run.fail=Unable to run replication listener
 NioReceiver.start.fail=Unable to start cluster receiver
 NioReceiver.stop.fail=Unable to close cluster receiver selector
+NioReceiver.stop.threadRunning=The NioReceiver thread did not stop in a timely 
manner. Errors may be observed when the selector is closed.
 NioReceiver.threadpool.fail=ThreadPool cannot be initialized. Listener not 
started.
 NioReceiver.threadsExhausted=Channel key is registered, but has had no 
interest ops for the last [{0}] ms. (cancelled: [{1}]):[{2}] last access:[{3} 
Possible cause: all threads used, perform thread dump
 

Modified: 
tomcat/trunk/java/org/apache/catalina/tribes/transport/nio/NioReceiver.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/tribes/transport/nio/NioReceiver.java?rev=1498363&r1=1498362&r2=1498363&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/tribes/transport/nio/NioReceiver.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/tribes/transport/nio/NioReceiver.java 
Mon Jul  1 10:57:50 2013
@@ -55,6 +55,8 @@ public class NioReceiver extends Receive
     protected static final StringManager sm =
             StringManager.getManager(Constants.Package);
 
+    private volatile boolean running = false;
+
     private AtomicReference<Selector> selector = new AtomicReference<>();
     private ServerSocketChannel serverChannel = null;
     private DatagramChannel datagramChannel = null;
@@ -351,7 +353,17 @@ public class NioReceiver extends Receive
         Selector selector = this.selector.get();
         if (selector != null) {
             try {
+                // Unlock the thread if is is blocked waiting for input
                 selector.wakeup();
+                // Wait for the receiver thread to finish
+                int count = 0;
+                while (running && count < 50) {
+                    Thread.sleep(100);
+                    count ++;
+                }
+                if (running) {
+                    log.warn(sm.getString("NioReceiver.stop.threadRunning"));
+                }
                 closeSelector();
             } catch (Exception x) {
                 log.error(sm.getString("NioReceiver.stop.fail"), x);
@@ -403,10 +415,13 @@ public class NioReceiver extends Receive
      */
     @Override
     public void run() {
+        running = true;
         try {
             listen();
         } catch (Exception x) {
             log.error(sm.getString("NioReceiver.run.fail"), x);
+        } finally {
+            running = false;
         }
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to