Repository navigation
fix(pglite-socket): release the connection slot when a handler errors - #1160
Merged
Merged
Conversation
A handler that ends in an error (ECONNRESET, idle timeout, a failed write) detaches itself and removes the socket's close listener, so it never emits 'close' and the server never removed it from its handler set. Each such connection used up a slot for good, and once maxConnections was reached every new connection was refused. Remove the handler on 'error' as well as on 'close'. Fixes electric-sql#1155
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
tdrz
self-requested a review
October 26, 2026 08:02
Contributor
Author
|
Thanks for the quick review and merge, @tdrz! Happy to pick up other pglite-socket issues if there's anything you'd like help with. |
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 #1155
Problem
PGLiteSocketServeronly removes a connection's handler when the handler emitsclose. A handler that ends in an error never emitsclose, so its slot is never freed. Oncehandlers.sizereachesmaxConnections, every new connection getsToo many connectionsuntil the server restarts. A client process that exits with a connection still open (ECONNRESET on the server) is enough to trigger it. So is a connection that hitsidleTimeout. Prisma'sprisma devusesmaxConnections: 10, so ten such exits lock it up.Root cause
PGLiteSocketHandler.handleError()dispatcheserrorand then callsdetach(true).detach()callssocket.removeAllListeners('close')before it destroys the socket, sohandleClose()never runs andcloseis never dispatched. InhandleConnection(), the server'serrorlistener only logs. Only thecloselistener callsthis.handlers.delete(handler).Change
PGLiteSocketServer.handleConnection(): the handler'serrorlistener now also removes the handler fromthis.handlers. Deleting twice is a no-op, so theclosepath is unchanged.@electric-sql/pglite-socket.I kept the handler's own event semantics as they are (error-ended handlers still don't emit
close), so anyone listening toPGLiteSocketHandlerevents directly sees no change.Tests
tests/connection-cleanup.test.ts, which uses real TCP sockets and realpgclients:frees the slot when a client disconnects with ECONNRESET:maxConnections: 2, three clients connect, run a query, then reset the socket (resetAndDestroy()). Each slot must be released, and a fresh client must then connect and run a query.frees the slot when a connection hits the idle timeout:maxConnections: 1, idleTimeout: 100. An idle socket gets timed out, its slot must be released, and a new client must connect.main(e752dbe):activeConnectionsnever drops back to 0. Both pass with this change, and passed 5 out of 5 repeated runs.mainshowed the same thing. WithmaxConnections: 3and five clients that reset their sockets,activeConnectionsclimbed 1, 2, 3, and then clients 4 and 5 and a fresh client were all refused. With the fix,activeConnectionsstays at 0 and the fresh client works.vitest runinpackages/pglite-socket(the package'stestscript): 7 files, 65 tests passed. That's the 63 existing tests plus the 2 new ones.tsc,eslint ./src ./tests --max-warnings 0andprettier --check ./src ./testsare clean.This change was made with AI assistance; the tests listed above were run locally before opening this PR.