|
|
880c94 |
--- java/org/apache/tomcat/websocket/WsSession.java.org 2017-03-09 14:51:41.000000000 +0100
|
|
|
880c94 |
+++ java/org/apache/tomcat/websocket/WsSession.java 2018-07-18 17:37:46.853657200 +0200
|
|
|
880c94 |
@@ -595,8 +595,8 @@
|
|
|
880c94 |
localEndpoint.onError(this, e);
|
|
|
880c94 |
}
|
|
|
880c94 |
}
|
|
|
880c94 |
-
|
|
|
880c94 |
-
|
|
|
880c94 |
+
|
|
|
880c94 |
+
|
|
|
880c94 |
/**
|
|
|
880c94 |
* Use protected so unit tests can access this method directly.
|
|
|
880c94 |
*/
|
|
|
880c94 |
@@ -635,29 +635,48 @@
|
|
|
880c94 |
* {@link FutureToSendHandler} completes.
|
|
|
880c94 |
*/
|
|
|
880c94 |
protected void registerFuture(FutureToSendHandler f2sh) {
|
|
|
880c94 |
- boolean fail = false;
|
|
|
880c94 |
- synchronized (stateLock) {
|
|
|
880c94 |
- // If the session has already been closed the any registered futures
|
|
|
880c94 |
- // will have been processed so the failure result for this future
|
|
|
880c94 |
- // needs to be set here.
|
|
|
880c94 |
- if (state == State.OPEN || f2sh.isCloseMessage()) {
|
|
|
880c94 |
- // WebSocket session is open or this is the close message
|
|
|
880c94 |
- futures.put(f2sh, f2sh);
|
|
|
880c94 |
- } else if (f2sh.isDone()) {
|
|
|
880c94 |
- // NO-OP. The future completed before the session closed so no
|
|
|
880c94 |
- // need to register in case the session closes before it
|
|
|
880c94 |
- // completes.
|
|
|
880c94 |
- } else {
|
|
|
880c94 |
- // Construct the exception outside of the sync block
|
|
|
880c94 |
- fail = true;
|
|
|
880c94 |
- }
|
|
|
880c94 |
+ // Ideally, this code should sync on stateLock so that the correct
|
|
|
880c94 |
+ // action is taken based on the current state of the connection.
|
|
|
880c94 |
+ // However, a sync on stateLock can't be used here as it will create the
|
|
|
880c94 |
+ // possibility of a dead-lock. See BZ 61183.
|
|
|
880c94 |
+ // Therefore, a slightly less efficient approach is used.
|
|
|
880c94 |
+
|
|
|
880c94 |
+ // Always register the future.
|
|
|
880c94 |
+ futures.put(f2sh, f2sh);
|
|
|
880c94 |
+
|
|
|
880c94 |
+ if (state == State.OPEN || f2sh.isCloseMessage()) {
|
|
|
880c94 |
+ // The session is open. The future has been registered with the open
|
|
|
880c94 |
+ // session. Normal processing continues.
|
|
|
880c94 |
+ return;
|
|
|
880c94 |
}
|
|
|
880c94 |
|
|
|
880c94 |
- if (fail) {
|
|
|
880c94 |
- IOException ioe = new IOException(sm.getString("wsSession.messageFailed"));
|
|
|
880c94 |
- SendResult sr = new SendResult(ioe);
|
|
|
880c94 |
- f2sh.onResult(sr);
|
|
|
880c94 |
+ // The session is closed. The future may or may not have been registered
|
|
|
880c94 |
+ // in time for it to be processed during session closure.
|
|
|
880c94 |
+
|
|
|
880c94 |
+ if (f2sh.isDone()) {
|
|
|
880c94 |
+ // The future has completed. It is not known if the future was
|
|
|
880c94 |
+ // completed normally by the I/O layer or in error by doClose(). It
|
|
|
880c94 |
+ // doesn't matter which. There is nothing more to do here.
|
|
|
880c94 |
+ return;
|
|
|
880c94 |
}
|
|
|
880c94 |
+
|
|
|
880c94 |
+ // The session is closed. The Future had not completed when last checked.
|
|
|
880c94 |
+ // There is a small timing window that means the Future may have been
|
|
|
880c94 |
+ // completed since the last check. There is also the possibility that
|
|
|
880c94 |
+ // the Future was not registered in time to be cleaned up during session
|
|
|
880c94 |
+ // close.
|
|
|
880c94 |
+ // Attempt to complete the Future with an error result as this ensures
|
|
|
880c94 |
+ // that the Future completes and any client code waiting on it does not
|
|
|
880c94 |
+ // hang. It is slightly inefficient since the Future may have been
|
|
|
880c94 |
+ // completed in another thread or another thread may be about to
|
|
|
880c94 |
+ // complete the Future but knowing if this is the case requires the sync
|
|
|
880c94 |
+ // on stateLock (see above).
|
|
|
880c94 |
+ // Note: If multiple attempts are made to complete the Future, the
|
|
|
880c94 |
+ // second and subsequent attempts are ignored.
|
|
|
880c94 |
+
|
|
|
880c94 |
+ IOException ioe = new IOException(sm.getString("wsSession.messageFailed"));
|
|
|
880c94 |
+ SendResult sr = new SendResult(ioe);
|
|
|
880c94 |
+ f2sh.onResult(sr);
|
|
|
880c94 |
}
|
|
|
880c94 |
|
|
|
880c94 |
|