Skip to content

End a server-closed hibernatable WebSocket's connection after the close handshake - #8950

Open
exKAZUu wants to merge 2 commits into
cloudflare:mainfrom
exKAZUu:hibernatable-ws-close-handshake
Open

exKAZUu wants to merge 2 commits into
cloudflare:mainfrom
exKAZUu:hibernatable-ws-close-handshake

Conversation

@exKAZUu

@exKAZUu exKAZUu commented Nov 26, 2026

Copy link
Copy Markdown

When a Durable Object closes a hibernatable WebSocket (ctx.acceptWebSocket()) and the client answers with its Close, the connection stays open until the actor is evicted, about 10 seconds later, and indefinitely while the actor stays busy. Clients that wait for the server to end the connection report the close that much later: about 10 s for Node's WebSocket, 2 s for Chromium. A client that sent a data frame before the close is not affected, and neither is a WebSocket accepted with accept(). Fixes #7566.

Cause

The connection ends when the worker's end of the WebSocketPipe goes away; couple() relies on this ("the pipe keeps the incoming pump running after it delivers a Close, so the session still ends when the worker releases its end").

  • When the client's Close is the first message the pipe receives, BlockedReceive::tryPumpFrom() delivers it and then starts other.pumpTo(pipe) again, which waits until that end goes away. After an earlier data frame, the Close arrives through BlockedPumpFrom::receive() instead, which ends the pump on Close. That is the difference the issue observed.
  • For a hibernatable WebSocket, the pump continuation in ensurePumping() releases the native WebSocket only if the server's Close is still being sent when the client's arrives. When the server's Close went out first, initiateHibernatableRelease() marks the incoming side closed but nothing releases the native WebSocket, so its end of the pipe lives until the actor's IoContext is torn down.

I confirmed the sequence with temporary logging in a server-test case: with and without a preceding text frame, the hibernation manager receives the Close, initiateHibernatableRelease() runs with closedOutgoing = true and isPumping = false, and tryReleaseNative() is never called; only the connection without a data frame stays open.

Fix

LegacyWebSocketAdapter::initiateHibernatableRelease() now calls tryReleaseNative(), which releases the native WebSocket when our Close has already been sent and nothing is pumping, the same release the pump continuation performs. A client-initiated close is unaffected: closedOutgoing is still false then, and the handler's close() releases it after the reply is sent, as before.

Releasing moves the native state to Released, and getHibernatableTags() read the tags only from the Accepted state, so the adapter now keeps the tags once the release begins (releasedHibernatableTags) and returns them from there. ctx.getTags(ws) therefore keeps working in webSocketClose(), also after the handler awaits something. WrappedWebSocket::initiateHibernatableRelease() no longer stores a second copy.

Test

New server-test case: a Durable Object accepts a hibernatable WebSocket with a tag, a second request closes it with code 4001, the client answers with a Close and sends no data frame, and the connection must be at EOF one (virtual) second later, well before eviction. Its webSocketClose() awaits a timer and then reads ctx.getTags(ws), which the test checks through a later request. It fails without the fix (expected wsConn.isEof()) and passes with it.

Built and run in a Linux arm64 container (Debian bookworm), on this branch's base:

  • bazel test //src/workerd/server:server-test@ --test_arg=--filter=server-test.c++:4836-4840: fails without the fix, passes with it.
  • These pass with the fix: //src/workerd/server:server-test@ (all cases), //src/workerd/io:hibernation-manager-test@, and in //src/workerd/api/tests: websocket-hibernation@, tail-worker-test@, do-on-hibernate-symbol-test@, websocket-accept-limit-test@, websocket-accept-limit-killswitch-test@, websocket-allow-half-open-test@, websocket-buffered-amount-test@, websocket-client-error-test@, websocket-constructor-test@, websocket-handlers-test@, websocket-open-error-test@, websocket-send-before-open-test@, websocket-send-blob-test@, websocket-standard-url-test@.
  • The issue's reproduction with workerd serve and Node's WebSocket (server close(4001) to client close event):
Case workerd 1.20261123.1 This branch
hibernatable, no data frame 10018 ms 4 ms
hibernatable, text frame first 10 ms 14 ms
accept() 3 ms 3 ms

An async webSocketClose() handler that awaits 50 ms and then calls ctx.getTags(ws) still receives the tags, with readyState 3.

  • Formatted with the repository's clang-format (//build/deps/formatters:clang-format).

I did not run the @all-autogates and @all-compat-flags variants or anything on bazelflare.

Not covered

  • HibernatableWebSocketAdapter has its own initiateHibernatableRelease(), but it is not selected while the autogate consultation in WebSocket::acceptAsHibernatable() stays disabled (TODO(EW-10817)), so it is unchanged.
  • BlockedReceive::tryPumpFrom() in KJ continuing to pump after a Close, unlike pumpWebSocketLoop() and BlockedPumpFrom, is left alone; this change does not depend on it.

Generated with Claude Code

…se handshake

When a Durable Object closes a hibernatable WebSocket and the client
answers with its Close, the connection stayed open until the actor was
evicted, about 10 seconds later, or indefinitely while the actor stayed
busy. Clients that wait for the server to end the connection saw the
close event that much later.

The connection ends when the worker's end of the WebSocketPipe goes
away. When the client's Close is the first message the pipe receives,
the incoming pump delivers it and keeps pumping, so the pipe's end has to
be released. For a hibernatable WebSocket that happened in the pump
continuation only if the server's Close was still being sent when the
client's arrived. If the server's Close went out first, nothing released
it before eviction. A client that had sent a data frame first was not
affected, because the pipe then ends the incoming pump on Close.

initiateHibernatableRelease() now calls tryReleaseNative(), which
releases the native WebSocket once our Close has been sent and nothing
is pumping. Releasing leaves the native state Released, from which
getHibernatableTags() could not read, so the adapter keeps the tags once
the release begins. ctx.getTags(ws) keeps working in webSocketClose(),
also after the handler awaits something.
@exKAZUu
exKAZUu requested review from a team as code owners November 26, 2026 23:24
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@clanky-the-clanker

Copy link
Copy Markdown

This pull request was not reviewed automatically because its author does not have write access to this repository. A maintainer with write access can start a review by commenting clanky review (or clanker review or @clanky-the-clanker review), optionally followed by instructions (e.g. clanky review focus on the retry logic).

@exKAZUu

exKAZUu commented Nov 27, 2026

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hibernatable WebSocket: server-initiated close leaves the connection open for 10 s if the client never sent a data frame

1 participant