Skip to content

fix(ws_js): use close codes browsers accept - #576

Open
0pcom wants to merge 1 commit into
coder:masterfrom
0magnet:wasm-close-code
Open

0pcom wants to merge 1 commit into
coder:masterfrom
0magnet:wasm-close-code

Conversation

@0pcom

@0pcom 0pcom commented Oct 4, 2026

Copy link
Copy Markdown

Fixes #575

WebSocket.close throws for codes other than 1000 and 3000-4999. For those codes, exportedClose now sends 1000 to the browser. The close error the caller sees still carries the code they asked for. The peer receives 1000 rather than, say, 1001, because the browser API has no way to send the others.

The error handler also stops calling closeWithInternal. Before this change that call always threw on 1011 and returned at once. With a valid code it would wait for the close event inside the error callback, and that event cannot be delivered while the callback blocks, so the program deadlocks. A close event always follows an error event, so recording the error is enough. TestWasmDialTimeout caught this.

TestWasmCloseStatus closes with 1001, 1008, 1009 and 1011 and fails without the fix. I ran the wasm tests under Node (its WebSocket applies the same code check) against a local echo server, three times, all passing. I did not run them under wasmbrowsertest. go vet passes for both native and js, and the native tests pass.

This does not handle a close reason longer than 123 bytes, which the browser also rejects.

WebSocket.close throws for any code other than 1000 and 3000-4999, so
closing with the codes this package uses itself (1001 from CloseNow, 1008,
1009, 1011) failed and left the socket open, and under TinyGo the throw is
a fatal panic. Send 1000 for those codes while keeping the requested code
in the local close error, and stop the error handler from closing, since a
close event always follows and waiting for it inside the callback deadlocks.

This branch has not been deployed

No deployments
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.

wasm: Close with codes other than 1000 throws, so CloseNow never closes

1 participant