Skip to content

Commit ac30bcb

Browse files
committed
WebSocketServerSockJsSession uses dedicated disconnect lock
Issue: SPR-14917 (cherry picked from commit a49809b)
1 parent 872d6ef commit ac30bcb

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/WebSocketServerSockJsSession.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@
4242
* A SockJS session for use with the WebSocket transport.
4343
*
4444
* @author Rossen Stoyanchev
45+
* @author Juergen Hoeller
4546
* @since 4.0
4647
*/
4748
public class WebSocketServerSockJsSession extends AbstractSockJsSession implements NativeWebSocketSession {
@@ -54,6 +55,8 @@ public class WebSocketServerSockJsSession extends AbstractSockJsSession implemen
5455

5556
private final Object initSessionLock = new Object();
5657

58+
private final Object disconnectLock = new Object();
59+
5760
private volatile boolean disconnected;
5861

5962

@@ -136,18 +139,14 @@ private void checkDelegateSessionInitialized() {
136139

137140
@Override
138141
public Object getNativeSession() {
139-
if ((this.webSocketSession != null) && (this.webSocketSession instanceof NativeWebSocketSession)) {
140-
return ((NativeWebSocketSession) this.webSocketSession).getNativeSession();
141-
}
142-
return null;
142+
return (this.webSocketSession instanceof NativeWebSocketSession ?
143+
((NativeWebSocketSession) this.webSocketSession).getNativeSession() : null);
143144
}
144145

145146
@Override
146147
public <T> T getNativeSession(Class<T> requiredType) {
147-
if ((this.webSocketSession != null) && (this.webSocketSession instanceof NativeWebSocketSession)) {
148-
return ((NativeWebSocketSession) this.webSocketSession).getNativeSession(requiredType);
149-
}
150-
return null;
148+
return (this.webSocketSession instanceof NativeWebSocketSession ?
149+
((NativeWebSocketSession) this.webSocketSession).getNativeSession(requiredType) : null);
151150
}
152151

153152

@@ -166,7 +165,7 @@ public void initializeDelegateSession(WebSocketSession session) {
166165
scheduleHeartbeat();
167166
this.openFrameSent = true;
168167
}
169-
catch (Exception ex) {
168+
catch (Throwable ex) {
170169
tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
171170
}
172171
}
@@ -196,10 +195,8 @@ public void handleMessage(TextMessage message, WebSocketSession wsSession) throw
196195

197196
@Override
198197
public void sendMessageInternal(String message) throws SockJsTransportFailureException {
199-
200198
// Open frame not sent yet?
201199
// If in the session initialization thread, then cache, otherwise wait.
202-
203200
if (!this.openFrameSent) {
204201
synchronized (this.initSessionLock) {
205202
if (!this.openFrameSent) {
@@ -208,6 +205,7 @@ public void sendMessageInternal(String message) throws SockJsTransportFailureExc
208205
}
209206
}
210207
}
208+
211209
cancelHeartbeat();
212210
writeFrame(SockJsFrame.messageFrame(getMessageCodec(), message));
213211
scheduleHeartbeat();
@@ -224,10 +222,12 @@ protected void writeFrameInternal(SockJsFrame frame) throws IOException {
224222

225223
@Override
226224
protected void disconnect(CloseStatus status) throws IOException {
227-
synchronized (this) {
228-
if (isActive()) {
229-
this.disconnected = true;
230-
this.webSocketSession.close(status);
225+
if (isActive()) {
226+
synchronized (this.disconnectLock) {
227+
if (isActive()) {
228+
this.disconnected = true;
229+
this.webSocketSession.close(status);
230+
}
231231
}
232232
}
233233
}

0 commit comments

Comments
 (0)