Hibernatable WebSocket: server-initiated close leaves the connection open for 10 s if the client never sent a data frame
What versions & operating system are you using?
- workerd 2026-08-01 (npm
[email protected]) and workerd 2026-09-28 (npm[email protected]). Both behave the same. - Compatibility dates 2026-04-01 and 2026-04-07 (so with and without
web_socket_auto_reply_to_close). Both behave the same. - Linux x64 (kernel 6.8), Node 24.21.0,
ws8.21.0 as the client.
Please provide a link to a minimal reproduction
Inline below: three files, run with workerd serve directly. No wrangler or Miniflare.
Describe the Bug
A Durable Object accepts a WebSocket with ctx.acceptWebSocket() and later closes it with ws.close(1000, "..."). If the client has not sent any data frame on that socket, the connection stays open for about 10 s after the close handshake.
- The client receives the Close frame within about 70 ms and replies at once.
- The TCP connection then stays open. The client's
closeevent fires about 10,000 ms after the server'sclose(). - If the client sent one text frame before the close, the connection ends within about 10 ms.
- With
server.accept()(no hibernation API), the connection ends within about 10 ms either way.
Measured output (workerd 2026-09-28, compatibility date 2026-04-01):
hibernation, no data frame: close frame at 45 ms, close event 1000 "closed by server" at 10048 ms
hibernation, one text frame first: close frame at 4 ms, close event 1000 "closed by server" at 5 ms
accept(), no data frame: close frame at 5 ms, close event 1000 "closed by server" at 7 ms
accept(), one text frame first: close frame at 4 ms, close event 1000 "closed by server" at 7 msThe other three version and date combinations give the same pattern (10,052 to 10,104 ms for the first case). Nothing is written to workerd's log.
A client that sends nothing is common: a read-only subscriber, or a client the server disconnects before it sends anything. Each such close holds the connection open for 10 s.
Expected: after the closing handshake completes, the server ends the connection promptly, as it does when the client sent a data frame first, or when the socket was accepted with accept().
worker.mjs
import { DurableObject } from "cloudflare:workers";
export class Example extends DurableObject {
async fetch(request) {
const url = new URL(request.url);
if (url.pathname === "/close") {
const sockets = this.ctx.getWebSockets();
for (const ws of sockets) ws.close(1000, "closed by server");
return new Response(`closed ${sockets.length}`);
}
if (request.headers.get("Upgrade") !== "websocket") {
return new Response("Expected Upgrade: websocket", { status: 426 });
}
const [client, server] = Object.values(new WebSocketPair());
if (url.searchParams.get("mode") === "accept") {
server.accept();
this.plain = server;
server.addEventListener("message", () => {});
} else {
this.ctx.acceptWebSocket(server);
}
return new Response(null, { status: 101, webSocket: client });
}
async closePlain() {
this.plain?.close(1000, "closed by server");
}
webSocketMessage() {}
webSocketClose(ws, code, reason) {
ws.close(code, reason);
}
}
export default {
async fetch(request, env) {
const url = new URL(request.url);
const stub = env.EXAMPLE.get(
env.EXAMPLE.idFromName(url.searchParams.get("name") ?? "a")
);
if (url.pathname === "/close-plain") {
await stub.closePlain();
return new Response("closed plain");
}
return stub.fetch(request);
},
};config.capnp
using Workerd = import "/workerd/workerd.capnp";
const config :Workerd.Config = (
services = [ (name = "main", worker = .worker) ],
sockets = [ (name = "http", address = "127.0.0.1:8799", http = (), service = "main") ],
);
const worker :Workerd.Worker = (
modules = [ (name = "worker.mjs", esModule = embed "worker.mjs") ],
compatibilityDate = "2026-04-01",
durableObjectNamespaces = [ (className = "Example", uniqueKey = "example") ],
durableObjectStorage = (inMemory = void),
bindings = [ (name = "EXAMPLE", durableObjectNamespace = "Example") ],
);client.mjs
import WebSocket from "ws";
const base = "http://127.0.0.1:8799/";
async function measure(label, name, mode, sendFirst) {
const ws = new WebSocket(`ws://127.0.0.1:8799/?name=${name}&mode=${mode}`);
await new Promise((resolve, reject) => {
ws.once("open", resolve);
ws.once("error", reject);
});
if (sendFirst) ws.send("hello");
await new Promise((r) => setTimeout(r, 200));
const closed = new Promise((resolve) =>
ws.once("close", (code, reason) =>
resolve({ code, reason: reason.toString() })
)
);
const start = performance.now();
let frameMs = null;
const poll = setInterval(() => {
if (frameMs === null && ws.readyState === WebSocket.CLOSING) {
frameMs = Math.round(performance.now() - start);
}
}, 1);
const path = mode === "accept" ? "close-plain" : "close";
await (await fetch(`${base}${path}?name=${name}`)).text();
const outcome = await closed;
clearInterval(poll);
const ms = Math.round(performance.now() - start);
console.log(
`${label}: close frame at ${frameMs ?? "?"} ms, close event ${outcome.code} "${outcome.reason}" at ${ms} ms`
);
}
await measure("hibernation, no data frame", "a", "hibernate", false);
await measure("hibernation, one text frame first", "b", "hibernate", true);
await measure("accept(), no data frame", "c", "accept", false);
await measure("accept(), one text frame first", "d", "accept", true);Steps
npm i [email protected] [email protected]
npx workerd serve config.capnp &
node client.mjs"close frame at" is when the ws client moves to CLOSING, which it does as soon as it receives the server's Close frame and replies. "close event" is when the TCP connection ends.
Source: cloudflare/workerd