Skip to content

fix(world-postgres): remove stream reader listeners on EOF, query failure, and close - #3158

Open
Mohith26 wants to merge 2 commits into
vercel:mainfrom
Mohith26:fix/world-postgres-listener-leaks
Open

fix(world-postgres): remove stream reader listeners on EOF, query failure, and close#3158
Mohith26 wants to merge 2 commits into
vercel:mainfrom
Mohith26:fix/world-postgres-listener-leaks

Conversation

@Mohith26

Copy link
Copy Markdown

Fixes #3120

What

world-postgres stream readers registered an events.on('strm:<name>', onData) listener whose cleanup only ran on explicit ReadableStream.cancel(). EOF close, initial-query rejection, and createStreamer().close() all left listeners attached, so repeated reads of a completed or failing stream accumulated listeners until MaxListenersExceededWarning, and closed readers were retained.

Each reader now owns a single idempotent cleanup invoked from all four terminal paths, and the streamer tracks active reader cleanups so close() detaches everything that remains. No public API changes.

Tests

Mock-based unit tests (fake pg client, no live Postgres) assert listener counts return to zero on all four paths, including 12 repeated EOF reads with no warnings. With the fix stashed, 3 of the 4 new tests fail with exactly the leaked counts; with it, the package suite is 30/30. Typecheck and biome clean.

Credit to @Jiarui-Ni for the detailed report in #3120.

Mohith26 added 2 commits July 28, 2026 11:39
…lure, and close

streams.get() registered an EventEmitter listener per reader but only
removed it when the consumer explicitly cancelled the ReadableStream.
Three other terminal paths leaked the listener: a persisted EOF chunk
closing the controller, the initial chunk query rejecting, and
createStreamer().close() (used by world.close()).

Give each reader a single idempotent cleanup that is invoked from all
four terminal paths, and track active reader cleanups in the streamer
so close() drains them. Repeated reads of a completed or failing
stream no longer accumulate listeners or trigger
MaxListenersExceededWarning.

Fixes vercel#3120
…minal paths

Add unit tests around createStreamer() with a mocked pg client and a
minimal fake drizzle chain, capturing the internal EventEmitter via a
prototype spy to assert listener counts:

- 12 repeated reads to EOF leave zero listeners and emit no
  MaxListenersExceededWarning
- a rejecting initial chunk query removes the listener
- streamer close() detaches 10 active (uncancelled) readers
- explicit consumer cancellation still cleans up (control)
@Mohith26
Mohith26 requested review from a team and ijjk as code owners July 28, 2026 17:33
@changeset-bot

changeset-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: abee9f1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@vercel

vercel Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

@Mohith26 is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

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.

world-postgres: stream readers leak EventEmitter listeners after EOF, query errors, and World close

1 participant