Skip to content

fix(pg-cloudflare): do not emit an error for the read that end() cancels - #3806

Open
WolfieLeader wants to merge 1 commit into
brianc:masterfrom
WolfieLeader:fix/cloudflare-end-unhandled
Open

WolfieLeader wants to merge 1 commit into
brianc:masterfrom
WolfieLeader:fix/cloudflare-end-unhandled

Conversation

@WolfieLeader

Copy link
Copy Markdown

Fixes #3805.

Since #3747, CloudflareSocket.end() closes the socket while _listen() still waits on a read. workerd rejects that read with "This socket has been closed.", and pg re-emits it as a client 'error' after end() resolved. With no 'error' listener, that is an unhandled rejection.

This change ignores a rejected read once end() has been called. Other read failures still emit 'error'.

Tests:

  • New unit test in packages/pg-esm-test/pg-cloudflare.test.js. A fake reader keeps the read pending until the fake close() rejects it, as workerd does. Before: 36 pass, 1 fail. After: 37 pass.
  • workerd (Workers Vitest pool, pg 8.23.1, local Hyperdrive to PostgreSQL 18), await client.end() after a query:
    • 1.4.1: unhandled rejection in 3 of 3 runs.
    • This branch: clean in 3 of 3 runs.
    • A server-side disconnect (pg_terminate_backend) still reaches the client as 'error'.

Not changed: _listenOnce(), the SSL handshake read. In workerd, end() during the handshake did not reject that read.

Since brianc#3747, end() closes the Cloudflare socket while _listen() still
waits on a read. workerd rejects that read with "This socket has been
closed.", and pg re-emits it as a client 'error' after end() resolved.
With no 'error' listener this is an unhandled rejection.

Ignore a rejected read once end() has been called. Other read failures
still emit 'error'.
@WolfieLeader
WolfieLeader marked this pull request as ready for review October 6, 2026 14:55
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.

pg-cloudflare 1.4.1: client.end() emits "This socket has been closed." error

1 participant