Skip to content
Commit Detail

Commit 029973c

Author
Kenton Varda <kenton@cloudflare.com> 2025-01-20 11:58:35 -0600
Parents
c134deb
Tree
42a608f
Fix Sentry error: "expected !currentlySending; another message send is already in progress"

If writing to the underlying KJ WebSocket throws an exception or is canceled, then the WebSocket is left in an inconsistent state and no further messages can be sent. All further attempts to send messages will throw the above exception.

In the WebSocket workerd API code, upon catching an exception from a write, we normally set `outgoingAborted = true`, which causes us to drop all future writes on the floor.

The problem is, we weren't actually setting `outgoingAborted = true` until `reportError()` was invoked, which is a few microtasks after the point where `pump()` ended. In the meantime, a new `send()` could be invoked, starting a fresh `pump()`, which would then fail with the `expected !currentlySending` error.

To fix the problem, we must set `outgoingAborted = true` promptly on exit from `pump()`.

I was unable to reproduce the problem in a test, though, because the WebSocket API code also listens on the underlying WebSocket's `whenAborted()` and, when it resolves, immediately sets `outgoingAborted = true`. There must be some case where `send()` throws but `whenAborted()` doesn't actually fire, but I can't think of what that would be.

Nevertheless, it is quite clear that we need to set `outgoingAborted = true` promptly when a send fails, before anyone else could possibly begin another pump. So, this commit does that.

Files changed

1 file changed~1 modified