workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

"Uncaught (in response) Error: The script will never generate a response" is thrown when a WebSocket client disconnects

Open madurangae opened this issue 1 year ago • 5 comments

Which Cloudflare product(s) does this pertain to?

Workers Runtime

What version(s) of the tool(s) are you using?

3.39.0 [Wrangler]

What version of Node are you using?

v20.5.0

What operating system and version are you using?

Mac Sonoma 14.4.1

Describe the Bug

Observed behavior

"Uncaught (in response) Error: The script will never generate a response" is thrown when a WebSocket client disconnects

Expected behavior

No errors to be thrown

Steps to reproduce

Please provide the following:

  • A minimal working subset of your worker code
export default {
	async fetch(request: Request) {
		return await handleRequest(request);
	},
} satisfies ExportedHandler;

async function handleRequest(request: Request) {
	const upgradeHeader = request.headers.get('Upgrade');
	if (!upgradeHeader || upgradeHeader !== 'websocket') {
		return new Response('Expected Upgrade: websocket', { status: 426 });
	}

	const webSocketPair = new WebSocketPair();
	const [client, server] = Object.values(webSocketPair);

	server.accept();
	server.addEventListener('message', event => {
		console.log(event.data);
	});

	return new Response(null, {
		status: 101,
		webSocket: client,
	});
}
  • A minimal working subset of your wrangler.toml name = "lively-sky-8da1" main = "src/index.ts" compatibility_date = "2023-12-18"

  • Commands used to start your local dev server, including custom env and cli args npx wrangler dev

  • Steps to be performed in the browser, curl commands, or a test we can run that reliably fails (at least a percent of the time) Connect to the WebSocket endpoint (e.g websocat ws://localhost:8787/ws) Terminate the client process afterwards

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

npx wrangler dev ⛅️ wrangler 3.39.0

⎔ Starting local server... [wrangler:inf] Ready on http://localhost:8787 [wrangler:inf] GET /ws 101 Switching Protocols (4ms) ✘ [ERROR] Uncaught (in response) Error: The script will never generate a response.

madurangae avatar Mar 28 '24 20:03 madurangae

+1 seeing the same problem here

nc avatar Mar 30 '24 06:03 nc

In my case, closing the web socket from the server side will not cause this error. It will occur if it is closed by the client side. For example, the web socket will be automatically closed after the web page is closed.

worker (server side):

export default {
	async fetch(request) {
		if (request.headers.get("Upgrade") !== "websocket")
			return new Response("Expected Upgrade: websocket", { status: 426 });
		const [client, server] = Object.values(new WebSocketPair());
		server.accept();
		server.addEventListener("message", event => {
			console.log(event.data);
			if (event.data === "close") {
				server.close(); // close from the server side
			}
		});
		return new Response(null, { status: 101, webSocket: client, });
	},
};

web page in browser (client side):

const websocket = new WebSocket("ws://localhost:8787");
setTimeout(() => {
  // This will fire an error in cloudflare worker:
  // ✘ [ERROR] Uncaught (in response) Error: The script will never generate a response.
  // websocket.close();

  // this OK for me
  websocket.send("close");
}, 2000);

But this cannot solve the situation where the web page is closed unexpectedly. It is only a temporary solution. This is still a bug that needs to be resolved.

jerrychan7 avatar Apr 12 '24 10:04 jerrychan7

I am encountering the same problem, even when using the official worker-websocket templete.

In addition to the issue, The Close Event of the browser does not fire if I call ws.close() from the browser. I have to add websocket.close() in the close event handler of the server in order to signal to the browser to fire the close event.

Edit: I event modified the compatibility_date = "2024-04-20", and compatibility_flags = [ "nodejs_compat" ] Same Errors.

Please note that my Error Appears only if I didn't click the 'Click me' Button in the example.

Lan-Hekary avatar Apr 20 '24 22:04 Lan-Hekary

I'm seeing this as well–any updates?

smallbraineng avatar Jun 26 '24 00:06 smallbraineng

refs https://github.com/cloudflare/cloudflare-docs/pull/15014

irvinebroque avatar Jun 26 '24 01:06 irvinebroque

This error is expected if the client closes a websocket and the server (i.e. the Worker) does not handle that correctly. We have added a section to the docs (see https://github.com/cloudflare/cloudflare-docs/pull/15014) to explain this.

petebacondarwin avatar Jul 22 '24 13:07 petebacondarwin