Repository navigation
Conversation
59 files in server/tests opened a pool and never gave it back. bun test
runs every file in one process, and a pool opens its connections on
first use and holds them until closed, so the leaks accumulated for
the whole run against PostgreSQL's max_connections of 100, and the
failure ('sorry, too many clients already') landed on whoever added
the next file rather than on the file that leaked.
Each file now closes its pool in an afterAll registered last, so Bun's
first-registered-first ordering runs it after the file's own cleanup.
A guard test refuses a new file that opens a database without closing
it, and the sharded runner's header now says what the ceiling actually
is: a function of how many files have run, not imported, with sharding
kept for wall-clock rather than as the only thing under the ceiling.
Measured against PostgreSQL 14, all 59 files in one process: peak
connection count 83 before, 4 after; 758 tests pass either way.
Fixes CopilotKit#785
A2wkiller
requested review from
MikeRyanDev,
davidmckayv,
dustywusty,
guidovizoso,
mxmzb,
samjulien and
tylerslaton
as code owners
October 30, 2026 13:29
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #785.
What
59 files in
server/testsopened a database pool and never gave it back.bun testruns every file in one process, and a pool opens its connections on first use and holds them until it is closed, so the leaks accumulated for the whole run against PostgreSQL'smax_connectionsof 100. When the ceiling goes, the failure (sorry, too many clients already) lands on whoever adds the next file, not on the file that leaked - which is exactly what #770's CI run showed.Change
afterAllregistered last, so Bun's first-registered-first ordering runs it after the file's own cleanup - the idiom the already-closing files use.server/tests/database-pool-closing.test.ts) refuses a new file that opens a database without closing it. A guard rather than a lint rule because "opens and closes" is a pairing, andserver/testsis outsidetsc, so types would not catch it either.Tests
Measured against PostgreSQL 14 with all 59 files in one process:
The 5 files with nested
afterAllcleanup (plugin-store, enterprise-controls, shared-use-store, plugin-credential-binding, sandboxed-components) pass with their cleanups running before the close. Biome lint clean on all 61 changed files.Prepared with AI assistance (Instinct); tested as noted above.