From 7dc2b2927e0168080619839ba63db5c4e820a6da Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Sat, 28 Jun 2014 14:36:18 -0400 Subject: [PATCH] Use SESSION_NOT_RELIABLE when no messages received When a WebSocket session is closed after not having received any messages, we'll use SESSION_NOT_RELIABLE to indicate to other parts of the session closing code not to send anything further (e.g. SockJS "Go Away!" frame). Issue: SPR-11884 --- .../org/springframework/web/socket/CloseStatus.java | 9 ++++----- .../socket/messaging/SubProtocolWebSocketHandler.java | 11 ++++++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/CloseStatus.java b/spring-websocket/src/main/java/org/springframework/web/socket/CloseStatus.java index 9733e7f2a5..9ea0dc5652 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/CloseStatus.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/CloseStatus.java @@ -135,11 +135,10 @@ public final class CloseStatus { /** - * Indicates that a session has become unreliable (e.g. timed out while sending - * a message) and extra care should be exercised while closing the session in - * order to avoid locking additional threads. - * - *

NOTE: Spring Framework specific status code. + * A status code for use within the framework the indicate a session has + * become unreliable (e.g. timed out while sending a message) and extra + * care should be exercised, e.g. avoid sending any further data to the + * client that may be done during normal shutdown. */ public static final CloseStatus SESSION_NOT_RELIABLE = new CloseStatus(4500); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java index c8d624f520..4ffb57d509 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java @@ -356,8 +356,13 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler, } /** - * Periodically check sessions to ensure they have received at least one - * message or otherwise close them. + * When a session is connected through a higher-level protocol it has a chance + * to use heartbeat management to shut down sessions that are too slow to send + * or receive messages. However, after a WebSocketSession is established and + * before the higher level protocol is fully connected there is a possibility + * for sessions to hang. This method checks and closes any sessions that have + * been connected for more than 60 seconds without having received a single + * message. */ private void checkSessions() throws IOException { long currentTime = System.currentTimeMillis(); @@ -380,7 +385,7 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler, "Closing " + holder.getSession() + "."); } try { - session.close(CloseStatus.PROTOCOL_ERROR); + session.close(CloseStatus.SESSION_NOT_RELIABLE); } catch (Throwable t) { logger.error("Failure while closing " + session, t);