Author: markt
Date: Mon Jun 19 09:22:43 2017
New Revision: 1799164

URL: http://svn.apache.org/viewvc?rev=1799164&view=rev
Log:
Improve thread-safety of Futures used to report the result of sending WebSocket 
messages.

Modified:
    tomcat/trunk/java/org/apache/tomcat/websocket/FutureToSendHandler.java
    tomcat/trunk/webapps/docs/changelog.xml

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/FutureToSendHandler.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/FutureToSendHandler.java?rev=1799164&r1=1799163&r2=1799164&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/FutureToSendHandler.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/FutureToSendHandler.java Mon 
Jun 19 09:22:43 2017
@@ -21,6 +21,7 @@ import java.util.concurrent.ExecutionExc
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
 
 import javax.websocket.SendHandler;
 import javax.websocket.SendResult;
@@ -37,7 +38,7 @@ class FutureToSendHandler implements Fut
 
     private final CountDownLatch latch = new CountDownLatch(1);
     private final WsSession wsSession;
-    private volatile SendResult result = null;
+    private volatile AtomicReference<SendResult> result = new 
AtomicReference<>(null);
 
     public FutureToSendHandler(WsSession wsSession) {
         this.wsSession = wsSession;
@@ -48,8 +49,7 @@ class FutureToSendHandler implements Fut
 
     @Override
     public void onResult(SendResult result) {
-
-        this.result = result;
+        this.result.compareAndSet(null, result);
         latch.countDown();
     }
 
@@ -82,8 +82,8 @@ class FutureToSendHandler implements Fut
         } finally {
             wsSession.unregisterFuture(this);
         }
-        if (result.getException() != null) {
-            throw new ExecutionException(result.getException());
+        if (result.get().getException() != null) {
+            throw new ExecutionException(result.get().getException());
         }
         return null;
     }
@@ -104,12 +104,9 @@ class FutureToSendHandler implements Fut
             throw new 
TimeoutException(sm.getString("futureToSendHandler.timeout",
                     Long.valueOf(timeout), unit.toString().toLowerCase()));
         }
-        if (result.getException() != null) {
-            throw new ExecutionException(result.getException());
+        if (result.get().getException() != null) {
+            throw new ExecutionException(result.get().getException());
         }
         return null;
     }
 }
-
-
-

Modified: tomcat/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1799164&r1=1799163&r2=1799164&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Mon Jun 19 09:22:43 2017
@@ -183,6 +183,10 @@
         <code>PongMessage</code> does not implement
         <code>MessageHandler.Whole</code>. (rjung)
       </fix>
+      <fix>
+        Improve thread-safety of <code>Future</code>s used to report the result
+        of sending WebSocket messages. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Web applications">



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

Reply via email to