Skip to content

Commit 7dc2b29

Browse files
committed
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
1 parent 3af488a commit 7dc2b29

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/CloseStatus.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,10 @@ public final class CloseStatus {
135135

136136

137137
/**
138-
* Indicates that a session has become unreliable (e.g. timed out while sending
139-
* a message) and extra care should be exercised while closing the session in
140-
* order to avoid locking additional threads.
141-
*
142-
* <p><strong>NOTE:</strong> Spring Framework specific status code.
138+
* A status code for use within the framework the indicate a session has
139+
* become unreliable (e.g. timed out while sending a message) and extra
140+
* care should be exercised, e.g. avoid sending any further data to the
141+
* client that may be done during normal shutdown.
143142
*/
144143
public static final CloseStatus SESSION_NOT_RELIABLE = new CloseStatus(4500);
145144

spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,13 @@ private String resolveSessionId(Message<?> message) {
356356
}
357357

358358
/**
359-
* Periodically check sessions to ensure they have received at least one
360-
* message or otherwise close them.
359+
* When a session is connected through a higher-level protocol it has a chance
360+
* to use heartbeat management to shut down sessions that are too slow to send
361+
* or receive messages. However, after a WebSocketSession is established and
362+
* before the higher level protocol is fully connected there is a possibility
363+
* for sessions to hang. This method checks and closes any sessions that have
364+
* been connected for more than 60 seconds without having received a single
365+
* message.
361366
*/
362367
private void checkSessions() throws IOException {
363368
long currentTime = System.currentTimeMillis();
@@ -380,7 +385,7 @@ private void checkSessions() throws IOException {
380385
"Closing " + holder.getSession() + ".");
381386
}
382387
try {
383-
session.close(CloseStatus.PROTOCOL_ERROR);
388+
session.close(CloseStatus.SESSION_NOT_RELIABLE);
384389
}
385390
catch (Throwable t) {
386391
logger.error("Failure while closing " + session, t);

0 commit comments

Comments
 (0)