Skip to content

fix(pglite-socket): release the connection slot when a handler errors - #1160

Merged
tdrz merged 1 commit into
electric-sql:mainfrom
PrabalParihar:fix/socket-error-slot-1155
Oct 26, 2026
Merged

tdrz merged 1 commit into
electric-sql:mainfrom
PrabalParihar:fix/socket-error-slot-1155

Conversation

@PrabalParihar

Copy link
Copy Markdown
Contributor

Fixes #1155

Problem

PGLiteSocketServer only removes a connection's handler when the handler emits close. A handler that ends in an error never emits close, so its slot is never freed. Once handlers.size reaches maxConnections, every new connection gets Too many connections until 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 hits idleTimeout. Prisma's prisma dev uses maxConnections: 10, so ten such exits lock it up.

Root cause

PGLiteSocketHandler.handleError() dispatches error and then calls detach(true). detach() calls socket.removeAllListeners('close') before it destroys the socket, so handleClose() never runs and close is never dispatched. In handleConnection(), the server's error listener only logs. Only the close listener calls this.handlers.delete(handler).

Change

  • PGLiteSocketServer.handleConnection(): the handler's error listener now also removes the handler from this.handlers. Deleting twice is a no-op, so the close path is unchanged.
  • Patch changeset for @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 to PGLiteSocketHandler events directly sees no change.

Tests

  • Added tests/connection-cleanup.test.ts, which uses real TCP sockets and real pg clients:
    • 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.
    • Both fail on main (e752dbe): activeConnections never drops back to 0. Both pass with this change, and passed 5 out of 5 repeated runs.
  • A standalone repro on main showed the same thing. With maxConnections: 3 and five clients that reset their sockets, activeConnections climbed 1, 2, 3, and then clients 4 and 5 and a fresh client were all refused. With the fix, activeConnections stays at 0 and the fresh client works.
  • vitest run in packages/pglite-socket (the package's test script): 7 files, 65 tests passed. That's the 63 existing tests plus the 2 new ones.
  • tsc, eslint ./src ./tests --max-warnings 0 and prettier --check ./src ./tests are clean.

This change was made with AI assistance; the tests listed above were run locally before opening this PR.

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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-25T06:45:23.272009Z
We couldn’t complete this review. Comment @codex review to try again.
624a014 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@tdrz
tdrz self-requested a review October 26, 2026 08:02

@tdrz tdrz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@tdrz
tdrz merged commit e0585be into electric-sql:main Oct 26, 2026
14 of 17 checks passed
@PrabalParihar

Copy link
Copy Markdown
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.

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.

[BUG]: pglite-socket: handlers that end in an error are never removed, so maxConnections fills up and every new connection is refused

2 participants