Repository navigation
Conversation
…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.
|
All contributors have signed the CLA ✍️ ✅ |
|
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 |
Author
|
I have read the CLA Document and I hereby sign the CLA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'sWebSocket, 2 s for Chromium. A client that sent a data frame before the close is not affected, and neither is a WebSocket accepted withaccept(). Fixes #7566.Cause
The connection ends when the worker's end of the
WebSocketPipegoes 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").BlockedReceive::tryPumpFrom()delivers it and then startsother.pumpTo(pipe)again, which waits until that end goes away. After an earlier data frame, the Close arrives throughBlockedPumpFrom::receive()instead, which ends the pump on Close. That is the difference the issue observed.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'sIoContextis torn down.I confirmed the sequence with temporary logging in a
server-testcase: with and without a preceding text frame, the hibernation manager receives the Close,initiateHibernatableRelease()runs withclosedOutgoing = trueandisPumping = false, andtryReleaseNative()is never called; only the connection without a data frame stays open.Fix
LegacyWebSocketAdapter::initiateHibernatableRelease()now callstryReleaseNative(), 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:closedOutgoingis still false then, and the handler'sclose()releases it after the reply is sent, as before.Releasing moves the native state to
Released, andgetHibernatableTags()read the tags only from theAcceptedstate, so the adapter now keeps the tags once the release begins (releasedHibernatableTags) and returns them from there.ctx.getTags(ws)therefore keeps working inwebSocketClose(), also after the handler awaits something.WrappedWebSocket::initiateHibernatableRelease()no longer stores a second copy.Test
New
server-testcase: 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. ItswebSocketClose()awaits a timer and then readsctx.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.//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@.workerd serveand Node'sWebSocket(serverclose(4001)to clientcloseevent):workerd1.20261123.1accept()An async
webSocketClose()handler that awaits 50 ms and then callsctx.getTags(ws)still receives the tags, withreadyState3.//build/deps/formatters:clang-format).I did not run the
@all-autogatesand@all-compat-flagsvariants or anything on bazelflare.Not covered
HibernatableWebSocketAdapterhas its owninitiateHibernatableRelease(), but it is not selected while the autogate consultation inWebSocket::acceptAsHibernatable()stays disabled (TODO(EW-10817)), so it is unchanged.BlockedReceive::tryPumpFrom()in KJ continuing to pump after a Close, unlikepumpWebSocketLoop()andBlockedPumpFrom, is left alone; this change does not depend on it.Generated with Claude Code