From 977c5ca439a04fafac9029d726a89a91bfc01a31 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 2 Jul 2014 18:28:48 -0400 Subject: [PATCH] Synchronize request init in AbstractHttpSockJsSession Although unlikely in practice (but not impossible), the SockJS integration tests write a message while the request is initializing. This change adds synchronization around request intiailization for the SockJS HTTP sesion. This is a backport of: https://github.com/spring-projects/spring-framework/commit/59e02e63c427659e701cc371d210faf1a85c1d42 Issue: SPR-11916 --- .../session/AbstractHttpSockJsSession.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java index ba45d9e3a1d..a75348301fe 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java @@ -201,18 +201,20 @@ public abstract class AbstractHttpSockJsSession extends AbstractSockJsSession { this.frameFormat = frameFormat; this.asyncRequestControl = request.getAsyncRequestControl(response); - try { - // Let "our" handler know before sending the open frame to the remote handler - delegateConnectionEstablished(); - writePrelude(request, response); - writeFrame(SockJsFrame.openFrame()); - if (isStreaming() && !isClosed()) { - startAsyncRequest(); + synchronized (this.responseLock) { + try { + // Let "our" handler know before sending the open frame to the remote handler + delegateConnectionEstablished(); + writePrelude(request, response); + writeFrame(SockJsFrame.openFrame()); + if (isStreaming() && !isClosed()) { + startAsyncRequest(); + } + } + catch (Throwable ex) { + tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR); + throw new SockJsTransportFailureException("Failed to open session", getId(), ex); } - } - catch (Throwable ex) { - tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR); - throw new SockJsTransportFailureException("Failed to open session", getId(), ex); } }