Skip to content

Close the connection when an upgraded socket closes, on TS streams - #8937

Merged
danlapid merged 1 commit into
mainfrom
dlapid/ts-streams-starttls-close
Nov 27, 2026
Merged

danlapid merged 1 commit into
mainfrom
dlapid/ts-streams-starttls-close

Conversation

@danlapid

@danlapid danlapid commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Under typescript_implemented_streams, closing the socket that startTls() returned leaves the connection open. The peer never sees it end, so a peer that waits for EOF hangs.

Cause

startTls() and takeConnectionStream() 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-test opens a starttls tunnel to a Durable Object's connect() 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:

  • The new test passes 5 times in each of its three variants (@, @all-autogates, @all-compat-flags).
  • These socket tests pass in @ 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, and sockets-test.
  • With typescript_implemented_streams added to their configs, the same tests fail exactly as they do on main: http-socket-test:socketFetcherUnusable and six tls-nodejs-test cases ("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

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>
@danlapid
danlapid marked this pull request as ready for review November 27, 2026 23:52
@danlapid
danlapid requested review from a team as code owners November 27, 2026 23:52
@danlapid
danlapid merged commit 67861ea into main Nov 27, 2026
24 of 25 checks passed
@danlapid
danlapid deleted the dlapid/ts-streams-starttls-close branch November 27, 2026 23:53
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.

2 participants