Skip to content

Keep a CONNECT's startTls slot alive with its connection - #8789

Merged
danlapid merged 1 commit into
mainfrom
dlapid/starttls-slot-lifetime
Nov 21, 2026
Merged

danlapid merged 1 commit into
mainfrom
dlapid/starttls-slot-lifetime

Conversation

@danlapid

Copy link
Copy Markdown
Collaborator

connectImpl() heap-allocated the kj::TlsStarterCallback slot of a
secureTransport: 'starttls' socket, put only a reference to it in
kj::HttpConnectSettings, and moved the Own into
Socket::ConnectionData. That is an IoOwn, so the slot was freed
with the caller's IoContext. The connection can outlive that
context: the Socket's readable and writable hold their own
references to it, and new Response(socket.readable) keeps reading
from it after the request ends. kj's HttpClientAdapter forwards the
reference to the target's connect() and attaches that pending call
to the connection. A target that serves the CONNECT late, such as a
Durable Object in blockConcurrencyWhile(), then read the freed slot
in ServiceWorkerGlobalScope::connect(), and either wrote a
kj::Function into it (setupInternalTlsRendezvous()) or moved a
stale value out of it into its own Socket. This is only reachable
with the experimental connect_handler_starttls flag.

Fix

The slot's lifetime now follows the connection, which owns the only
call that can fill it in:

  • connectImpl() allocates the slot with kj::rc<>() and attaches a
    reference to request.connection, next to the HttpClient.
    Attachments are destroyed after the stream, so the slot outlives
    the pending call that holds the reference to it.
  • The Socket keeps its own reference (toOwn() into setupSocket(),
    whose ConnectionData already stores a kj::Rc), so startTls()
    works as before.
  • kj::HttpConnectSettings, global-scope.c++ and the Socket
    signatures are unchanged.

Testing

The new connect-handler-starttls-lifetime-test has a Worker open a
starttls tunnel to a Durable Object held in blockConcurrencyWhile(),
return new Response(socket.readable), and end its request. The test
then releases the Durable Object, which serves the tunnel and writes
back its socket.secureTransport. The release arrives as a stream
closing rather than as a promise settlement, since a settlement from
another request reaches the Durable Object only once its input gate
opens. Nothing sleeps.

  • Without the fix, the test fails 5/5 under ASan with a
    heap-use-after-free read in ServiceWorkerGlobalScope::connect(),
    freed by ~ConnectionData from ~IoContext. Without ASan it
    crashes with SIGSEGV, 5/5.
  • With the fix, it passes 5/5 with and without ASan, as do the
    connect-handler, connect-neuter, connect-half-open,
    sockets-connect-error, sockets-test, socket-proxy-to, socket-close,
    js-rpc-socket, http-socket, outbound-interceptor-socket and node
    net/tls tests, in all variants.
  • clang-tidy is clean on //src/workerd/io:io.

Interaction with #7362

#7362 rewrites the handler side of this wiring but keeps the raw
reference: takeConnectTlsUpgrade() copies settings.tlsStarter, and
connectImpl() still sets httpConnectSettings.tlsStarter = tlsStarter from a kj::heap. This change's sockets.c++ hunk
applies cleanly to #7362's head (897562b); only the
api/tests/BUILD.bazel hunk has an adjacent-context conflict. The
proxyTo() and setInboundTlsUpgrade() paths in #7362 that move
connData->tlsStarter out of ConnectionData keep working, since
the connection's reference keeps the slot alive independently.

🤖 Generated with Claude Code

connect() with secureTransport 'starttls' offers whoever serves the
CONNECT a slot to fill in with the socket's startTls() callback. The
slot was heap-allocated and owned by the Socket's ConnectionData,
while kj::HttpConnectSettings carried only a reference to it. The
ConnectionData is an IoOwn, so it went away with the caller's
IoContext. The connection can outlive that: the Socket's readable
and writable hold their own references to it, and a response body
keeps reading from it after the request has ended.

For a connection into another Worker, kj's HttpClientAdapter
forwards the settings to the target's connect() and attaches the
pending call to the connection. When that call is served late, as by
a Durable Object still in blockConcurrencyWhile(), the target's
ServiceWorkerGlobalScope::connect() read the freed slot, and
setupInternalTlsRendezvous() wrote a kj::Function into it. If the
freed memory had been reallocated with an engaged value, the target
moved it into its own Socket, whose teardown then called through it.

The slot is now refcounted, and connectImpl() attaches a reference
to it to the connection, so it lives as long as the connection, and
with it the pending call that can fill it in. The Socket holds
another reference, as before.

connect-handler-starttls-lifetime-test has a Worker open a starttls
tunnel to a blocked Durable Object, return the socket's readable as
its response, and end its request before the Durable Object serves
the tunnel. Without the fix, AddressSanitizer reports a
heap-use-after-free in ServiceWorkerGlobalScope::connect() on every
run (5 of 5), and a build without it crashes with a segmentation
fault (5 of 5).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danlapid
danlapid marked this pull request as ready for review November 21, 2026 21:24
@danlapid
danlapid requested review from a team as code owners November 21, 2026 21:24
@danlapid
danlapid enabled auto-merge November 21, 2026 21:25

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — This is a small, correct lifetime fix for the starttls slot, with a deterministic regression test. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The slot is now reference-counted and the connection keeps it alive, so the CONNECT target can no longer write into freed memory or read freed memory from it. The change adds no new exposure, and the path stays behind the experimental connect_handler_starttls flag.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found. The kj::rc slot, the extra reference attached to the connection and toOwn() into setupSocket() are correct and stay local to connectImpl().


⚡ Performance: ✅ LGTM

No issues found. The only added cost is one refcount increment per connect.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 2d461da4

@danlapid
danlapid merged commit fd9ec66 into main Nov 21, 2026
23 checks passed
@danlapid
danlapid deleted the dlapid/starttls-slot-lifetime branch November 21, 2026 21:30
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