Skip to content

startLocalProxy leaks every socket it opens #598

Description

@KasperiP

Bug Description

Connector.startLocalProxy() adds every socket pair it creates to this.sockets and never removes them when they close. In a long-running process this retains one TLSSocket (and its native OpenSSL SecureContext) per pooled database connection for the lifetime of the process.

Root cause

src/connector.ts, in startLocalProxy:

server.on('connection', c => {
  const s = stream();
  this.sockets.add(s);   // TLS socket to Cloud SQL
  this.sockets.add(c);   // local unix domain socket
  c.pipe(s);
  s.pipe(c);
});

Both sockets are added to the Set and nothing ever removes them. The Set is only read by close(), so entries for long-closed connections stay reachable forever keeping each TLSSocket alive along with its native SecureContext.

The library already solves this correctly elsewhere. Example CloudSQLInstance.addSocket in src/cloud-sql-instance.ts:

this.sockets.add(socket);
// When the socket is closed, remove it.
socket.once('closed', () => {
  this.sockets.delete(socket);
});

That cleanup was never applied to the startLocalProxy path.

Also I think the readme example of usage with Prisma should be updated. Prisma now supports driver adapters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions