Skip to content

Commit

Permalink
WebSockets: don't set outgoingAborted if outgoing is closed
Browse files Browse the repository at this point in the history
If the WebSocket is already closed for writes, there's no need to set
outgoingAborted because there can be no more outgoing traffic.

This was showing up on protocol errors: the script would get an error
event describing what their WebSocket client said wrong (good), and
then a second error event complaining about a disconnect (bad).
  • Loading branch information
smerritt committed Jul 11, 2023
1 parent bb13e9b commit df9776c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/workerd/api/web-socket.c++
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,10 @@ kj::Promise<void> WebSocket::Accepted::createAbortTask(Native& native, IoContext
LOG_EXCEPTION("webSocketWhenAborted", e);
}
}).then([this, &native]() {
// Other end disconnected prematurely. We may be able to clean up our state.
native.outgoingAborted = true;
if (!native.closedOutgoing) {
// Other end disconnected prematurely. We may be able to clean up our state.
native.outgoingAborted = true;
}
if (!native.isPumping && native.closedIncoming) {
// We can safely destroy the underlying WebSocket as it is no longer in use.
// HACK: Replacing the state will delete `whenAbortedTask`, which is the task that is
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/server/server-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@ KJ_TEST("Server: test WebSocket errors: bad RSV bits") {
` server.addEventListener('message', (event) => {
` if (event.data === "getErrors") {
` server.send(JSON.stringify(errors))
` } else if (event.data === "getErrorCount") {
` server.send(errors.length.toString())
` } else {
` server.send(event.data); // echo
` }
Expand All @@ -676,6 +678,8 @@ KJ_TEST("Server: test WebSocket errors: bad RSV bits") {

auto errWsConn = test.connect("test-addr");
errWsConn.upgradeToWebSocket();
errWsConn.send("\x81\x0dgetErrorCount");
errWsConn.recvWebSocketRegex("1");
errWsConn.send("\x81\x09getErrors");
errWsConn.recvWebSocketRegex(".*RSV bits.*");
}
Expand Down

0 comments on commit df9776c

Please sign in to comment.