cloudflare / cloudflare/workerd

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

Open
#2,569 2 comments 3 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
8.7k
Forks
739
Avg merge
2d 20h
Merged PRs (30d)
174

Description

### 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:

```ts
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.