Repository navigation
Conversation
🦋 Changeset detectedLatest commit: a35ee92 The changes in this PR will be included in the next version bump. This PR includes changesets to release 8 packages
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 |
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
@cloudflare/autoconfig
@cloudflare/build-output-utils
@cloudflare/codemods
@cloudflare/config
@cloudflare/containers-shared
create-cloudflare
@cloudflare/deploy-helpers
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-functions
@cloudflare/pages-shared
@cloudflare/runtime-types
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-plugin
@cloudflare/workers-auth
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
|
@Pduhard - I have merged your other PR. Can you rebase this one? |
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
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]>
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]>
aead726 to
1399046
Compare
| 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. |
There was a problem hiding this comment.
This description on the details seems slightly verbose. Can we slim it down a bit to the key details.
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Fixed in ea82fe8, the reply timer is cleared when the socket closes.
| expect(uncaught).toEqual([]); | ||
| } finally { | ||
| process.off("uncaughtException", collect); | ||
| server.close(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Same helper as above (ea82fe8), close() destroys the accepted sockets.
| 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 }); | ||
| }); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Done in ea82fe8, the helper rejects on error and removes the listener once it is listening.
| }), | ||
| "hello" | ||
| ); | ||
| expect(response.length).toBeGreaterThan(0); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
`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]>
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
|
Glad this is fixed, and thank you for following up! 🙏 |
Fixes #15768.
The Hyperdrive proxy has no
errorlistener on the client socket, so a client socket error crashes the Node process. Two windows, plus one leak:pipe()re-emits a client error when nothing else listens, so it still crashes. Every pipe site now goes throughpipeSockets, which tears down both ends on error.pipeSocketsdestroys it at once. No test: that window can't be made deterministic.pipeSocketsreplacessetupTLSConnection.createPlainTCPConnectionno longer sets its own error handler, so it drops itsclientSocketparameter.proxy.spec.tscovers the two windows: both tests fail onmainand pass here.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.