From e764c8d897ef32b7c34c9b1f963e62d0eea217a8 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 26 Nov 2013 11:55:53 -0500 Subject: [PATCH] Fix synchronization issue in StompSubProtocolHandler Two concurrent threads should not send a message on a single WebSocket session at the same time, for example see: http://docs.oracle.com/javaee/7/api/javax/websocket/RemoteEndpoint.Basic.html In StompSubProtocolHandler it is quite possible that multiple messages may be broadcast to a single WebSocket client concurrently. This change adds synchronization around the sending of a message to a specific cilent session. Issue: SPR-11023 --- .../web/socket/messaging/StompSubProtocolHandler.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java index 0034f2d105b..0f6b388ca63 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java @@ -174,7 +174,11 @@ public class StompSubProtocolHandler implements SubProtocolHandler { try { message = MessageBuilder.withPayload(message.getPayload()).setHeaders(headers).build(); byte[] bytes = this.stompEncoder.encode((Message) message); - session.sendMessage(new TextMessage(new String(bytes, Charset.forName("UTF-8")))); + + synchronized(session) { + session.sendMessage(new TextMessage(new String(bytes, Charset.forName("UTF-8")))); + } + } catch (Throwable t) { sendErrorMessage(session, t);