workerd icon indicating copy to clipboard operation
workerd copied to clipboard

🐛 BUG: WebSocket without Durable Object stays in CLOSING state after close

Open RaresAil opened this issue 1 year ago • 0 comments

Which Cloudflare product(s) does this pertain to?

Workers Runtime

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

3.58.0 [Wranger]

What version of Node are you using?

22.2

What operating system and version are you using?

macOS Sonoma 14.5

Describe the Bug

Observed behavior

When closing the connection from the client, the server stays in COLOSING for more than 40s

Expected behavior

To change the state to CLOSED

Steps to reproduce

Please provide the following:

Follow the steps for the server here: https://developers.cloudflare.com/workers/examples/websockets/ and run it from postman, and on the close event watch for the readyState which stays on CLOSING for more than 40s and the postman is on Disconneting for 30s and after an error is thrown with No closing frame

Example code:

const upgradeHeader = c.req.header('Upgrade');
    if (!upgradeHeader || upgradeHeader !== 'websocket') {
      return new Response('Expected Upgrade: websocket', { status: 426 });
    }

    const webSocketPair = new WebSocketPair();
    const client = webSocketPair[0];
    const server = webSocketPair[1];

    server.accept();

    server.addEventListener(
      'close',
      (e) => {
        console.log('close', e, server.readyState);
        setTimeout(() => console.log(server.readyState), 5000);
      },
      {
        once: true
      }
    );

    server.addEventListener(
      'error',
      (e) => {
        console.log('error', e, server.readyState);
      },
      {
        once: true
      }
    );

    server.addEventListener('message', (e) => {
      console.log(e);
    });

    return new Response(null, {
      status: 101,
      webSocket: client
    });
    ```


### Workaround

To call again .close in the close event handler and suppress the connection lost error


### Please provide a link to a minimal reproduction

_No response_

### Please provide any relevant error logs

Error from postman after 30s: 1006 Abnormal Closure: No close frame was received.

RaresAil avatar Jun 03 '24 08:06 RaresAil