Repository navigation
fix(runtime): never block the JS thread in WebSocket send/close - #57
Merged
Merged
Conversation
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.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WebSocket.send()calledWinHttpWebSocketSendsynchronously 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()andclose()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:
bufferedAmountreports the bytes queued and not yet written.errorevent plus aclosewith code 1006.close()the server never answers closes with 1006 after 20 s.closeevent, also when its thread ends early.send()after the socket has finished is ignored.openis ignored afterclose()during CONNECTING.openis 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.sendreturns at once, messages keep arriving whilebufferedAmount > 0, and the server's FIN gives one error and one close (1006). Fails onmain.native_websocket_close_timeout_and_no_messages_after_close: one close (1006) after the timeout, no messages afterclose(). Fails onmain.native_websocket_uses_tcp_nodelay,native_websocket_round_trip.cargo check -p runtime --no-default-features --features module_runnerbuilds. The HTTP module-loader tests pass, which cover thewinhttp.rsrefactor.