Repository navigation
Close the connection when an upgraded socket closes, on TS streams - #8937
Merged
Merged
Conversation
startTls() and takeConnectionStream() neutralize the socket's readable by detaching it, and drop the detached copy. Under the C++ streams implementation that frees the copy, and its source's reference to the connection, at once. Under typescript_implemented_streams the copy is a JavaScript object whose native source holds the connection until the object is garbage-collected, so closing the socket that startTls() returned left the connection open and the peer never saw it end. Cancel the detached copy instead of dropping it. That releases its source right away under both implementations. The cancellation only abandons the old socket's own reads; it doesn't touch the connection, which now belongs to the new socket. The new test closes an upgraded socket and checks that the peer's read ends, under both implementations. Under TypeScript streams it hung before this change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jasnell
approved these changes
Nov 26, 2026
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.
Under
typescript_implemented_streams, closing the socket thatstartTls()returned leaves the connection open. The peer never sees it end, so a peer that waits for EOF hangs.Cause
startTls()andtakeConnectionStream()neutralize the old socket's readable by detaching it, and drop the detached copy. Under the C++ streams implementation, dropping the copy frees it at once, and with it its source's reference to the connection. Under TypeScript streams the copy is a JavaScript object, and its native source holds the connection until that object is garbage-collected. The writable side already handles this:WritableStreamNativeSink::detach()releases its sink eagerly.Fix
Socket::detachStreams()now cancels the detached readable instead of dropping it, which releases its source right away under both implementations. The cancellation reaches only the old socket's own reads (EncodedAsyncInputStream::cancel()cancels the reads it has pending), not the connection, which the new socket now owns.Test
connect-handler-starttls-close-testopens astarttlstunnel to a Durable Object'sconnect()handler. Both ends upgrade, the test closes its upgraded socket, and the handler reports through RPC how its pending read ended. The test runs the same worker under both implementations. Both now report the same disconnect error. Before this change the TypeScript run hung until the test timed out.Checked on bazelflare:
@,@all-autogates,@all-compat-flags).@and@all-compat-flags:connect-handler-test,connect-handler-starttls-lifetime-test,http-socket-test,socket-close-gc-test,socket-proxy-to-test,sockets-connect-error-test,outbound-interceptor-socket-test,js-rpc-socket-test,starttls-nodejs-test,tls-nodejs-test,net-nodejs-test, andsockets-test.typescript_implemented_streamsadded to their configs, the same tests fail exactly as they do on main:http-socket-test:socketFetcherUnusableand sixtls-nodejs-testcases ("The strategy for a byte stream cannot have a size function"). Those are separate TypeScript-streams gaps, listed in Let hang detection see TS-streams deadlocks with in-isolate JS #8938.Not covered
When a
connect()handler closes its upgraded socket, the caller still sees no EOF until the handler returns. This happens under both implementations, so it's a different issue and this PR leaves it alone.🤖 Generated with Claude Code