-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[UNDERTOW-2532] Fix NPE when transmitting text or binary message to websocket session at the same time #1708
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,24 +17,24 @@ | |
*/ | ||
package io.undertow.websockets.jsr; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.Future; | ||
|
||
import io.undertow.websockets.core.BinaryOutputStream; | ||
import io.undertow.websockets.core.StreamSinkFrameChannel; | ||
import io.undertow.websockets.core.WebSocketCallback; | ||
import io.undertow.websockets.core.WebSocketFrameType; | ||
import io.undertow.websockets.core.WebSocketUtils; | ||
import io.undertow.websockets.core.WebSockets; | ||
import org.xnio.channels.Channels; | ||
|
||
import jakarta.websocket.EncodeException; | ||
import jakarta.websocket.RemoteEndpoint; | ||
import jakarta.websocket.SendHandler; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.Future; | ||
import org.xnio.channels.Channels; | ||
|
||
/** | ||
* {@link RemoteEndpoint} implementation which uses a WebSocketSession for all its operation. | ||
|
@@ -243,7 +243,7 @@ class BasicWebSocketSessionRemoteEndpoint implements Basic { | |
private StreamSinkFrameChannel binaryFrameSender; | ||
private StreamSinkFrameChannel textFrameSender; | ||
|
||
public void assertNotInFragment() { | ||
public synchronized void assertNotInFragment() { | ||
if (textFrameSender != null || binaryFrameSender != null) { | ||
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage(); | ||
} | ||
|
@@ -268,57 +268,78 @@ public void sendBinary(final ByteBuffer data) throws IOException { | |
data.clear(); //for some reason the TCK expects this, might as well just match the RI behaviour | ||
} | ||
|
||
@Override | ||
@Override | ||
public void sendText(final String partialMessage, final boolean isLast) throws IOException { | ||
if(partialMessage == null) { | ||
if (partialMessage == null) { | ||
throw JsrWebSocketMessages.MESSAGES.messageInNull(); | ||
} | ||
if (binaryFrameSender != null) { | ||
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage(); | ||
} | ||
if (textFrameSender == null) { | ||
textFrameSender = undertowSession.getWebSocketChannel().send(WebSocketFrameType.TEXT); | ||
} | ||
|
||
StreamSinkFrameChannel sender = getTextFrameSender(); | ||
|
||
try { | ||
Channels.writeBlocking(textFrameSender, WebSocketUtils.fromUtf8String(partialMessage)); | ||
if(isLast) { | ||
textFrameSender.shutdownWrites(); | ||
Channels.writeBlocking(sender, WebSocketUtils.fromUtf8String(partialMessage)); | ||
if (isLast) { | ||
sender.shutdownWrites(); | ||
} | ||
Channels.flushBlocking(textFrameSender); | ||
Channels.flushBlocking(sender); | ||
} finally { | ||
if (isLast) { | ||
textFrameSender = null; | ||
clearTextFrameSender(); | ||
} | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void sendBinary(final ByteBuffer partialByte, final boolean isLast) throws IOException { | ||
|
||
if(partialByte == null) { | ||
throw JsrWebSocketMessages.MESSAGES.messageInNull(); | ||
} | ||
if (textFrameSender != null) { | ||
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage(); | ||
} | ||
if (binaryFrameSender == null) { | ||
binaryFrameSender = undertowSession.getWebSocketChannel().send(WebSocketFrameType.BINARY); | ||
} | ||
|
||
StreamSinkFrameChannel sender = getBinaryFrameSender(); | ||
|
||
try { | ||
Channels.writeBlocking(binaryFrameSender, partialByte); | ||
if(isLast) { | ||
binaryFrameSender.shutdownWrites(); | ||
Channels.writeBlocking(sender, partialByte); | ||
if (isLast) { | ||
sender.shutdownWrites(); | ||
} | ||
Channels.flushBlocking(binaryFrameSender); | ||
} finally { | ||
Channels.flushBlocking(sender); | ||
} | ||
finally { | ||
if (isLast) { | ||
binaryFrameSender = null; | ||
clearBinaryFrameSender(); | ||
} | ||
} | ||
partialByte.clear(); | ||
} | ||
|
||
private synchronized StreamSinkFrameChannel getTextFrameSender() throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two different senders are guarded by single instance lock. Is there a reason for such arrangement? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @baranowb If we were to apply separate locks for Additionally, other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @baranowb |
||
if (binaryFrameSender != null) { | ||
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage(); | ||
} | ||
if (textFrameSender == null) { | ||
textFrameSender = undertowSession.getWebSocketChannel().send(WebSocketFrameType.TEXT); | ||
} | ||
return textFrameSender; | ||
} | ||
|
||
private synchronized void clearTextFrameSender() { | ||
textFrameSender = null; | ||
} | ||
|
||
private synchronized StreamSinkFrameChannel getBinaryFrameSender() throws IOException { | ||
if (textFrameSender != null) { | ||
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage(); | ||
} | ||
if (binaryFrameSender == null) { | ||
binaryFrameSender = undertowSession.getWebSocketChannel().send(WebSocketFrameType.BINARY); | ||
} | ||
return binaryFrameSender; | ||
} | ||
|
||
private synchronized void clearBinaryFrameSender() { | ||
binaryFrameSender = null; | ||
} | ||
|
||
@Override | ||
public OutputStream getSendStream() throws IOException { | ||
assertNotInFragment(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im going to talk about this with @ropalka . But it seems its technically possible for competing send to undercut one another, though I might be missing how this piece is being used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@baranowb
I would like to share my understanding and seek your feedback.
For example, in the case of sending a Text message, I assume that a single message is divided into multiple frames for transmission. In this process, the
TextFrameSender
class variable is used to callsendText(partialMessage, isLast)
multiple times, sending partial messages through the same channel. (During this process, othersend()
methods are not allowed to send messages.)However, if partial frames are being sent from multiple threads, I believe there is a possibility that one thread might set
textFrameSender
to null first, and then another thread could callChannels.flushBlocking(StreamSinkFrameChannel)
, leading to a NullPointerException (NPE).To address this, I have modified the code to ensure synchronization when accessing the shared resource, the
textFrameSender
class variable.Additionally, could you please clarify what specific scenarios you had in mind regarding
'competing send to undercut one another'
?Understanding this will help me consider those cases and make further improvements accordingly.