Skip to content

fix: Clean up sockets when closed in startLocalProxy and CloudSQLInstance - #605

Merged
kgala2 merged 1 commit into
mainfrom
gh-fix-startlocalproxy-socket-leak
Aug 18, 2026
Merged

fix: Clean up sockets when closed in startLocalProxy and CloudSQLInstance#605
kgala2 merged 1 commit into
mainfrom
gh-fix-startlocalproxy-socket-leak

Conversation

@hessjcg

@hessjcg hessjcg commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

Fixes #598

Description

When Connector.startLocalProxy() creates socket pairs (the local Unix domain socket connection and the TLS connection to Cloud SQL), the sockets were added to this.sockets but never removed upon closing. In long-running processes, closed sockets and TLS contexts were retained indefinitely.

Additionally, CloudSQLInstance.addSocket was listening for a 'closed' event instead of the standard Node.js 'close' event, preventing closed sockets attached to domain-name instances from being cleaned up.

Changes

  • Registered 'close' event listeners on both client sockets and stream sockets in Connector.startLocalProxy() to delete them from this.sockets when closed.
  • Registered 'close' event listener on the local proxy server to delete it from this.localProxies when closed.
  • Fixed the close event name in CloudSQLInstance.addSocket from 'closed' to 'close'.
  • Added unit tests for startLocalProxy and CloudSQLInstance.addSocket verifying socket cleanup upon close.

…ance

When Connector.startLocalProxy() creates socket pairs (the local domain
socket and the TLS connection to Cloud SQL), the sockets were added to
this.sockets but never removed upon closing. Over time in a long-running
process, closed sockets and TLS contexts were leaked.

Similarly, CloudSQLInstance.addSocket was listening to 'closed' event
instead of Node.js standard 'close' event, preventing closed sockets from
being removed.

This fix:
- Attaches 'close' event listeners to remove both client and stream sockets
  from Connector.sockets when they close.
- Cleans up server from Connector.localProxies upon 'close'.
- Fixes the event name in CloudSQLInstance.addSocket from 'closed' to 'close'.
- Adds unit test coverage for startLocalProxy and addSocket socket cleanup.

BUG=545159743
TAG=agy
CONV=badc8593-ab83-4543-91f6-a49f1997305a
@hessjcg
hessjcg marked this pull request as ready for review August 18, 2026 04:24
@hessjcg
hessjcg requested a review from a team as a code owner August 18, 2026 04:24
@kgala2
kgala2 merged commit 0f6ef2a into main Aug 18, 2026
28 checks passed
@kgala2
kgala2 deleted the gh-fix-startlocalproxy-socket-leak branch August 18, 2026 16:12
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.

startLocalProxy leaks every socket it opens

2 participants