Skip to content

Commit

Permalink
fix(ext/websocket): don't throw exception when sending to closed socket
Browse files Browse the repository at this point in the history
  • Loading branch information
bellinitte committed Nov 19, 2024
1 parent 186b527 commit 712b60d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
6 changes: 5 additions & 1 deletion ext/websocket/01_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,14 @@ class WebSocket extends EventTarget {
webidl.requiredArguments(arguments.length, 1, prefix);
data = webidl.converters.WebSocketSend(data, prefix, "Argument 1");

if (this[_readyState] !== OPEN) {
if (this[_readyState] === CONNECTING) {
throw new DOMException("'readyState' not OPEN", "InvalidStateError");
}

if (this[_readyState] !== OPEN) {
return;
}

if (this[_sendQueue].length === 0) {
// Fast path if the send queue is empty, for example when only synchronous
// data is being sent.
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/websocket_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,3 +806,18 @@ Deno.test("Close connection", async () => {
await server.finished;
conn.close();
});

Deno.test("send to a closed socket", async () => {
const { promise, resolve } = Promise.withResolvers<void>();
const ws = new WebSocket("ws://localhost:4242");
const blob = new Blob(["foo"]);
ws.onerror = () => fail();
ws.onopen = () => {
ws.close();
ws.send(blob);
};
ws.onclose = () => {
resolve();
};
await promise;
});
10 changes: 5 additions & 5 deletions tests/wpt/runner/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -11518,11 +11518,11 @@
"006.html?default": true,
"006.html?wpt_flags=h2": false,
"006.html?wss": false,
"007.html?default": false,
"007.html?wpt_flags=h2": false,
"007.html?wss": false,
"008.html?default": false,
"008.html?wss": false,
"007.html?default": true,
"007.html?wpt_flags=h2": true,
"007.html?wss": true,
"008.html?default": true,
"008.html?wss": true,
"009.html?default": {
"ignore": true
},
Expand Down

0 comments on commit 712b60d

Please sign in to comment.