Skip to content

Commit 3a105d1

Browse files
committed
[UNDERTOW-2532] Modify it to be thread safe when transmitting text or binary message to websocket session
1 parent a4720e3 commit 3a105d1

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

websockets-jsr/src/main/java/io/undertow/websockets/jsr/WebSocketSessionRemoteEndpoint.java

+40-33
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,10 @@ class BasicWebSocketSessionRemoteEndpoint implements Basic {
244244
private StreamSinkFrameChannel textFrameSender;
245245

246246
public void assertNotInFragment() {
247-
if (textFrameSender != null || binaryFrameSender != null) {
248-
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage();
247+
synchronized (this) {
248+
if (textFrameSender != null || binaryFrameSender != null) {
249+
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage();
250+
}
249251
}
250252
}
251253

@@ -273,50 +275,55 @@ public void sendText(final String partialMessage, final boolean isLast) throws I
273275
if(partialMessage == null) {
274276
throw JsrWebSocketMessages.MESSAGES.messageInNull();
275277
}
276-
if (binaryFrameSender != null) {
277-
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage();
278-
}
279-
if (textFrameSender == null) {
280-
textFrameSender = undertowSession.getWebSocketChannel().send(WebSocketFrameType.TEXT);
281-
}
282-
try {
283-
Channels.writeBlocking(textFrameSender, WebSocketUtils.fromUtf8String(partialMessage));
284-
if(isLast) {
285-
textFrameSender.shutdownWrites();
278+
279+
synchronized (this) {
280+
if (binaryFrameSender != null) {
281+
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage();
282+
}
283+
if(textFrameSender == null) {
284+
textFrameSender = undertowSession.getWebSocketChannel().send(WebSocketFrameType.TEXT);
286285
}
287-
Channels.flushBlocking(textFrameSender);
288-
} finally {
289-
if (isLast) {
290-
textFrameSender = null;
286+
try {
287+
Channels.writeBlocking(textFrameSender, WebSocketUtils.fromUtf8String(partialMessage));
288+
if(isLast) {
289+
textFrameSender.shutdownWrites();
290+
}
291+
Channels.flushBlocking(textFrameSender);
292+
} finally {
293+
if(isLast) {
294+
textFrameSender = null;
295+
}
291296
}
292297
}
293-
294298
}
295299

296300
@Override
297301
public void sendBinary(final ByteBuffer partialByte, final boolean isLast) throws IOException {
298-
299302
if(partialByte == null) {
300303
throw JsrWebSocketMessages.MESSAGES.messageInNull();
301304
}
302-
if (textFrameSender != null) {
303-
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage();
304-
}
305-
if (binaryFrameSender == null) {
306-
binaryFrameSender = undertowSession.getWebSocketChannel().send(WebSocketFrameType.BINARY);
307-
}
308-
try {
309-
Channels.writeBlocking(binaryFrameSender, partialByte);
310-
if(isLast) {
311-
binaryFrameSender.shutdownWrites();
305+
306+
synchronized (this) {
307+
if (textFrameSender != null) {
308+
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage();
309+
}
310+
if (binaryFrameSender == null) {
311+
binaryFrameSender = undertowSession.getWebSocketChannel().send(WebSocketFrameType.BINARY);
312+
}
313+
try {
314+
Channels.writeBlocking(binaryFrameSender, partialByte);
315+
if (isLast) {
316+
binaryFrameSender.shutdownWrites();
317+
}
318+
Channels.flushBlocking(binaryFrameSender);
312319
}
313-
Channels.flushBlocking(binaryFrameSender);
314-
} finally {
315-
if (isLast) {
316-
binaryFrameSender = null;
320+
finally {
321+
if (isLast) {
322+
binaryFrameSender = null;
323+
}
317324
}
325+
partialByte.clear();
318326
}
319-
partialByte.clear();
320327
}
321328

322329
@Override

0 commit comments

Comments
 (0)