Skip to content

api: keep Hyperdrive subrequest channel alive for the CONNECT stream - #7719

Closed
bartlomieju wants to merge 4 commits into
cloudflare:mainfrom
bartlomieju:fix/hyperdrive-worker-designator
Closed

bartlomieju wants to merge 4 commits into
cloudflare:mainfrom
bartlomieju:fix/hyperdrive-worker-designator

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Hyperdrive::connectToDb() created a raw HTTP client over the subrequest
channel and returned the connection stream, letting both the client and the
channel die with the coroutine frame.

When the designator resolves to a Worker service, the channel owns the
WorkerEntrypoint serving the CONNECT, whose deferred drain later
reads waitUntilTasks on a destroyed object and crashes the process with
SIGSEGV.

This PR attaches client and channel to the returned stream so they live as
long as the connection.

Fixes #6901

@bartlomieju
bartlomieju requested review from a team as code owners October 13, 2026 11:16

@mar-cf mar-cf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm with some cleanup

Comment thread src/workerd/api/hyperdrive.c++ Resolved
Comment thread src/workerd/server/server-test.c++ Resolved

@danlapid danlapid left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ugh you are so right, I even fixed an internal version of this a few years ago and forgot about this

Hyperdrive::connectToDb() created a raw HTTP client over the subrequest
channel and returned the connection stream, letting both the client and
the channel die with the coroutine frame. When the designator resolves
to a Worker service, the channel owns the WorkerEntrypoint serving the
CONNECT, whose deferred drain later reads waitUntilTasks on a destroyed
object and crashes the process with SIGSEGV. Attach client and channel
to the returned stream so they live as long as the connection.

Fixes cloudflare#6901
@mar-cf
mar-cf force-pushed the fix/hyperdrive-worker-designator branch from 3ad8164 to 6416f6e Compare October 16, 2026 11:13
@clanky-the-clanker

clanky-the-clanker Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

✅ Approved — The fix makes the Hyperdrive CONNECT stream own its HTTP client and subrequest channel, so the WorkerEntrypoint is no longer freed while it is still serving the stream. A regression test covers the crash, and no issues were found.

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


✅ All checks passed

📋 Code Quality: ✅ LGTM

I found no issues in either changed file.

  • Fix (src/workerd/api/hyperdrive.c++:139-146): asHttpClient() takes ownership of the subrequest channel (worker-interface.c++:117-119). The client is then attached to the returned connection stream, so the channel, and the WorkerEntrypoint serving the CONNECT, now live as long as the stream. Because of the attach order, the stream is destroyed before the client and channel, which is correct.
  • Error path: when the status is not 2xx, the client stays a local in the coroutine frame, so it is still alive while errorBody is read.
  • Test (src/workerd/server/server-test.c++): the new test sets up a Hyperdrive designator that resolves to a Worker service with a connect handler. It checks that the echoed payload comes back and that the process does not crash, which is the scenario reported in Hyperdrive.connect() crashes the whole process with SIGSEGV when the binding's designator points at a Worker service instead of an external service #6901.

📝 Documentation: ✅ LGTM

No documentation changes are needed. The description explains the root cause and the fix clearly.


Reviewed commit: 07ac7b07

@jasnell

jasnell commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

The internal build won't run since this was opened from a fork. If you re-open this from a branch in cloudflare/workerd it'll run the internal build... or, if you just want to run the internal build manually to confirm nothing break i'm happy landing this with a bypass. Just let me know which you'd rather do.

@bartlomieju

Copy link
Copy Markdown
Collaborator Author

Superseded by #7908, opened from a branch in this repository so CI can run.

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.

Hyperdrive.connect() crashes the whole process with SIGSEGV when the binding's designator points at a Worker service instead of an external service

4 participants