Skip to content

Closing WebSockets from the server results in 'Close received after close' error #21642

@jespertheend

Description

@jespertheend

Version: Deno 1.39.0

This is similar to #20948 except that in this case WebSocket.close() is called from Deno's end.
While #20948 was fixed in #20518, this issue still seems to be reproducible.
This too seems to have regressed somewhere between 1.32.3 and 1.32.4.

To reproduce, run the following with deno run --unstable -A foo.js:

Deno.serve({hostname: "0.0.0.0", port: 8000}, (request) => {
	const url = new URL(request.url);
	if (url.pathname == "/") {
		return new Response(`
			<script>
				const url = new URL(location.href);
				url.pathname = "/ws";
				url.protocol = "ws:";
				const ws = new WebSocket(url.href);
			</script>
			<button onclick="ws.send('close')">close</button>
		`, {
			headers: {
				"Content-Type": "text/html",
			}
		});
	} else if (url.pathname == "/ws") {
		const { socket, response } = Deno.upgradeWebSocket(request);
		socket.addEventListener("open", () => {
			console.log("open");
		});
		socket.addEventListener("message", () => {
			console.log("close");
			socket.close();
		});
		return response;
	}
	return new Response("not found", {status: 404})
});

Then visit http://localhost:8000/ and click the 'close' button.

The browser console logs:

WebSocket connection to 'ws://localhost:8000/ws' failed: Close received after close

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions