Skip to content

fix(runtime): never block the JS thread in WebSocket send/close - #57

Merged
triniwiz merged 1 commit into
mainfrom
fix/websocket-nonblocking-send
Nov 21, 2026
Merged

triniwiz merged 1 commit into
mainfrom
fix/websocket-nonblocking-send

Conversation

@triniwiz

Copy link
Copy Markdown
Member

WebSocket.send() called WinHttpWebSocketSend synchronously on the JS thread, which is the app's UI thread. When the uplink stalled and the socket's send buffer filled, or the network dropped, the app stopped responding until TCP gave up.

Each socket's thread now drives WinHTTP in async mode: the handshake, one pending receive, and one send at a time from an ordered queue. send() and close() only queue a command. Closing a stuck socket's handles cancels its pending operations, so no thread outlives its socket.

Along with it, the WebSocket now behaves as in browsers:

  • bufferedAmount reports the bytes queued and not yet written.
  • A failed read or write is an error event plus a close with code 1006.
  • A close() the server never answers closes with 1006 after 20 s.
  • Each socket gets exactly one close event, also when its thread ends early.
  • send() after the socket has finished is ignored.
  • open is ignored after close() during CONNECTING.
  • The protocol reported on open is the one the server selected.

WinHTTP has no TCP_NODELAY option, but sets it on its own sockets; a test checks that.

Testing

Windows 11 x64, cargo test -p runtime --lib websocket: 5/5.

  • native_websocket_reads_while_writes_stall: the server never reads while the client queues 48 MiB. send returns at once, messages keep arriving while bufferedAmount > 0, and the server's FIN gives one error and one close (1006). Fails on main.
  • native_websocket_close_timeout_and_no_messages_after_close: one close (1006) after the timeout, no messages after close(). Fails on main.
  • native_websocket_uses_tcp_nodelay, native_websocket_round_trip.

cargo check -p runtime --no-default-features --features module_runner builds. The HTTP module-loader tests pass, which cover the winhttp.rs refactor.

send() called WinHttpWebSocketSend synchronously on the JS thread, which
is the app's UI thread. When the uplink stalled and the socket's send
buffer filled (or the network dropped), the UI hung until TCP gave up.

Each socket's thread now drives WinHTTP in async mode: the handshake,
one receive always pending, and one send at a time from a queue
(WinHTTP allows one outstanding send next to a receive). send() and
close() only queue a command, as in browsers. Also:
- bufferedAmount reports bytes queued and not yet written
- a failed read or write is an error event plus close 1006
- close() the server never answers closes with 1006 after 20 s
  (Firefox's timeout); stuck connections are torn down by closing
  their handles, so no thread outlives its socket
- exactly one close event per socket, even if the thread ends early
- send() after the socket finished is dropped silently
- the open event is ignored after close() during CONNECTING
- the protocol reported on open is the one the server selected

WinHTTP has no TCP_NODELAY option but already sets it on its sockets;
a test checks that. New tests stall the server's reads (send returns
at once, reads keep arriving, FIN surfaces as error + one close) and
cover the close timeout.
@coderabbitai

coderabbitai Bot commented Nov 21, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4f9232d5-dcb7-49f8-b8c2-423a78c4fa46

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@triniwiz
triniwiz merged commit b1a25b2 into main Nov 21, 2026
3 checks passed
@triniwiz
triniwiz deleted the fix/websocket-nonblocking-send branch November 21, 2026 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant