Skip to content

[miniflare] Handle client socket errors in the Hyperdrive proxy - #15769

Closed
Pduhard wants to merge 10 commits into
cloudflare:mainfrom
Pduhard:fix/hyperdrive-proxy-client-socket-errors
Closed

Pduhard wants to merge 10 commits into
cloudflare:mainfrom
Pduhard:fix/hyperdrive-proxy-client-socket-errors

Conversation

@Pduhard

@Pduhard Pduhard commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #15768.

The Hyperdrive proxy has no error listener on the client socket, so a client socket error crashes the Node process. Two windows, plus one leak:

  • After the pipes: pipe() re-emits a client error when nothing else listens, so it still crashes. Every pipe site now goes through pipeSockets, which tears down both ends on error.
  • During negotiation (TLS paths): the client socket has no listener before the pipes exist. One is attached when the connection is accepted.
  • Leak on the fallback paths: the database socket can open after the client has errored, and then stays open. pipeSockets destroys it at once. No test: that window can't be made deterministic.

pipeSockets replaces setupTLSConnection. createPlainTCPConnection no longer sets its own error handler, so it drops its clientSocket parameter.

proxy.spec.ts covers the two windows: both tests fail on main and pass here.


  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: internal socket lifecycle change, no public API surface touched.

Note

This is a contribution from an AI agent: Claude Code, Claude Opus 5. A human reviewed the diff and ran the reproduction, but wrote neither the patch nor this description.


Devin Review

@changeset-bot

changeset-bot Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a35ee92

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
miniflare Patch
@cloudflare/deploy-helpers Patch
@cloudflare/pages-shared Patch
@cloudflare/remote-bindings Patch
@cloudflare/runtime-types Patch
@cloudflare/vite-plugin Patch
@cloudflare/vitest-plugin Patch
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-project-automation github-project-automation Bot moved this to Untriaged in workers-sdk Sep 22, 2026
@workers-devprod
workers-devprod requested review from a team and NuroDev and removed request for a team September 22, 2026 10:05
@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/hyperdrive-proxy-client-socket-errors.md: [@cloudflare/wrangler]
  • packages/miniflare/src/plugins/hyperdrive/hyperdrive-proxy.ts: [@cloudflare/wrangler]
  • packages/miniflare/test/plugins/hyperdrive/proxy.spec.ts: [@cloudflare/wrangler]

devin-ai-integration[bot]

This comment was marked as resolved.

@pkg-pr-new

pkg-pr-new Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
@cloudflare/autoconfig

npm i https://pkg.pr.new/@cloudflare/autoconfig@15769

@cloudflare/build-output-utils

npm i https://pkg.pr.new/@cloudflare/build-output-utils@15769

@cloudflare/codemods

npm i https://pkg.pr.new/@cloudflare/codemods@15769

@cloudflare/config

npm i https://pkg.pr.new/@cloudflare/config@15769

@cloudflare/containers-shared

npm i https://pkg.pr.new/@cloudflare/containers-shared@15769

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@15769

@cloudflare/deploy-helpers

npm i https://pkg.pr.new/@cloudflare/deploy-helpers@15769

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@15769

miniflare

npm i https://pkg.pr.new/miniflare@15769

@cloudflare/pages-functions

npm i https://pkg.pr.new/@cloudflare/pages-functions@15769

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@15769

@cloudflare/runtime-types

npm i https://pkg.pr.new/@cloudflare/runtime-types@15769

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@15769

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@15769

@cloudflare/vitest-plugin

npm i https://pkg.pr.new/@cloudflare/vitest-plugin@15769

@cloudflare/workers-auth

npm i https://pkg.pr.new/@cloudflare/workers-auth@15769

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@15769

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@15769

wrangler

npm i https://pkg.pr.new/wrangler@15769

commit: a35ee92

devin-ai-integration[bot]

This comment was marked as resolved.

@petebacondarwin

Copy link
Copy Markdown
Contributor

@Pduhard - I have merged your other PR. Can you rebase this one?

@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/hyperdrive-proxy-client-socket-errors.md: [@cloudflare/wrangler]
  • packages/miniflare/src/plugins/hyperdrive/hyperdrive-proxy.ts: [@cloudflare/wrangler]
  • packages/miniflare/test/plugins/hyperdrive/proxy.spec.ts: [@cloudflare/wrangler]

Pduhard and others added 6 commits October 7, 2026 16:41
The local Hyperdrive proxy pipes the client socket and the database socket
together. Only the database side had an `error` listener, so an error on the
client socket had no listener and was thrown as an uncaught exception, taking
down the whole Node process.

Both sockets now go through a single helper that pipes them together and tears
down each side when either one errors.

Co-Authored-By: Claude Opus 5 <[email protected]>
On the TLS paths, #handleConnection writes to the database, reads the reply
and runs a TLS handshake before it reaches a pipe site, and the client socket
has no error listener for that whole window. A client that resets
mid-negotiation still took the Node process down.

The listener is now attached as soon as the connection is accepted, alongside
the per-pipe teardown that knows which peer to destroy.

setupTLSConnection had become a pass-through to pipeSockets, and
createPlainTCPConnection no longer needs a handler of its own now that its
callers pipe the socket it returns on the next line; both are gone.

Co-Authored-By: Claude Opus 5 <[email protected]>
On the fallback paths the database socket is opened after the negotiation, so
it can be created when the client has already errored. Its `error` and `close`
have fired by then, a listener attached at the pipe would never run, and
`pipe()` only ends the destination for a source that ended cleanly — leaving
the connection to the database open with nothing to serve.

No test: the window is a client error landing inside the TCP connect of the
replacement socket, which I could not make deterministic.

Co-Authored-By: Claude Opus 5 <[email protected]>
pipeSockets had the longest doc comment in the file on its smallest function,
most of it restating the two pipes and the two handlers below it. Only the
pipe() re-emit is worth writing down.

Co-Authored-By: Claude Opus 5 <[email protected]>
The entry described the internals on both sides of the fix, so it never said
what a user would have seen or whether they were affected. It now names the
symptom, the surfaces it killed, and the mode that skips the proxy. It also
covers the fallback connection left open, which the entry did not mention.

Co-Authored-By: Claude Opus 5 <[email protected]>
@Pduhard
Pduhard force-pushed the fix/hyperdrive-proxy-client-socket-errors branch from aead726 to 1399046 Compare October 7, 2026 14:53
Comment on lines +7 to +9
Miniflare's local Hyperdrive proxy watched for errors on the database connection but not on the one coming from the Worker. A Worker that hung up while the database was still writing, or a connection reset during TLS negotiation, raised an unhandled error that exited `wrangler dev`, `getPlatformProxy()` or a Vitest run.

The proxy now watches both sides for the whole life of the connection and closes one when the other fails. A connection opened towards the database after the Worker had already gone is now closed instead of left open. Connection strings using `sslmode=disable` are unaffected, since that mode connects directly and skips the proxy.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This description on the details seems slightly verbose. Can we slim it down a bit to the key details.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Slimmed it down to the symptom and the fix in a35ee92.

tlsSocket.on("error", () => socket.destroy());
tlsSocket.on("secure", () => {
const pump = setInterval(() => {
tlsSocket.write(Buffer.alloc(256 * 1024));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please respect backpressure here.

The interval allocates and writes another 256 KiB every millisecond even when write() returns false. If the proxy stops draining, buffering grows without bound; a paused-client reproduction queued 64 MiB in 300 ms.

Wait for drain before continuing, and reuse a single buffer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 884269a: one shared buffer, written until write() returns false and resumed on drain. With a paused client the queue now stays at a single chunk, where it was ~70 MiB after 300 ms.

expect(response.length).toBeGreaterThan(0);
} finally {
process.off("uncaughtException", collect);
server.close();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please explicitly clean up active sockets and pump timers during teardown.

server.close() stops accepting connections but leaves existing connections open. If the test times out with a TLS connection still active, its interval continues allocating after finally.

Track these resources and clear the intervals and destroy the sockets during cleanup.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in ea82fe8: both mock servers now start through a helper whose close() also destroys the sockets they accepted, which stops the pump.

const server = net.createServer((socket) => {
socket.on("error", () => {});
socket.once("data", () => {
setTimeout(() => socket.write("N"), delayMs);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please retain the timeout handle and clear it when the socket closes.

Currently, a disconnect leaves the timer running, and its callback later attempts to write "N" to a destroyed socket.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in ea82fe8, the reply timer is cleared when the socket closes.

expect(uncaught).toEqual([]);
} finally {
process.off("uncaughtException", collect);
server.close();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please destroy accepted sockets during teardown as well.

server.close() stops accepting connections but leaves existing connections open, so a failed test can leave mock database connections alive.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same helper as above (ea82fe8), close() destroys the accepted sockets.

Comment on lines +260 to +271
return new Promise((resolve) => {
const server = net.createServer((socket) => {
socket.on("error", () => {});
socket.once("data", () => {
setTimeout(() => socket.write("N"), delayMs);
});
});

server.listen(0, "127.0.0.1", () => {
const address = server.address() as net.AddressInfo;
resolve({ server, port: address.port });
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please reject the startup promise when the server emits an error.

Currently, a bind failure produces an uncaught error instead of rejecting the awaited setup. Add a temporary error listener and remove it once listening succeeds.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in ea82fe8, the helper rejects on error and removes the listener once it is listening.

}),
"hello"
);
expect(response.length).toBeGreaterThan(0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please assert the expected pump data on both connections.

TLS negotiation failures produce plaintext error responses, which satisfy the first data handler and this nonempty-response assertion. I forced TLS negotiation to fail and the test still passed, so it doesn’t reliably prove the streaming path was exercised.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, fixed in 110d06e. Both connections now assert they received the pump's bytes. I reran your check (the mock refusing SSL, and a cert mismatch under verify-full) and the test now fails with the plaintext error.

Pduhard and others added 4 commits October 8, 2026 22:19
`server.close()` stops accepting connections but leaves the accepted ones
open, so a test that failed or timed out could leave the pump writing and
the slow server's reply timer pending after its `finally`. Both mock servers
now start through one helper whose `close()` also destroys the sockets they
accepted, and the slow server clears its reply timer when its socket closes.

The helper also rejects when the server cannot listen, instead of the bind
error escaping as an uncaught exception.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The pump allocated and wrote a fresh 256 KiB buffer every millisecond
whatever `write()` returned, so a client that stopped reading made the
server's queue grow without bound: about 70 MiB in 300 ms with a paused
client. It now writes one shared buffer until `write()` returns `false` and
resumes on `drain`, which keeps the queue at a single chunk.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test only checked that each client received something, and a failed TLS
negotiation answers with a plaintext error, so it passed without the stream
ever flowing. Both connections now assert that what they received is the
pump's data. Forcing the negotiation to fail (the mock refusing SSL, or a
certificate that does not match under `verify-full`) now fails the test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
It went through the internals on both sides of the connection. It now says
what a user saw, where, and what the proxy does instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Pduhard
Pduhard requested a review from NuroDev October 16, 2026 12:12
@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/hyperdrive-proxy-client-socket-errors.md: [@cloudflare/wrangler]
  • packages/miniflare/src/plugins/hyperdrive/hyperdrive-proxy.ts: [@cloudflare/wrangler]
  • packages/miniflare/test/plugins/hyperdrive/proxy.spec.ts: [@cloudflare/wrangler]

@Pduhard

Pduhard commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Closing: main no longer crashes here. Both tests from this PR pass after #15879 and #14900. Thanks for the review :)

@Pduhard Pduhard closed this Oct 22, 2026
@github-project-automation github-project-automation Bot moved this from In Review to Done in workers-sdk Oct 22, 2026
@petebacondarwin

Copy link
Copy Markdown
Contributor

Glad this is fixed, and thank you for following up! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[miniflare] Hyperdrive proxy crashes the Node process when the client socket errors

4 participants