From 0d8c81303db18f7781123adaa8e2d1f05aeb26bb Mon Sep 17 00:00:00 2001 From: Jin Hou Date: Tue, 2 Jun 2026 13:08:24 -0700 Subject: [PATCH] fix: revert the bad fix from PR 566 --- src/cloud-sql-instance.ts | 30 ++++++++++++++---------------- src/connector.ts | 13 ++----------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/src/cloud-sql-instance.ts b/src/cloud-sql-instance.ts index 3dc2e329..23b1506b 100644 --- a/src/cloud-sql-instance.ts +++ b/src/cloud-sql-instance.ts @@ -364,14 +364,12 @@ export class CloudSQLInstance { this.checkDomainID = null; } for (const socket of this.sockets) { - if (typeof socket.destroy === 'function') { - socket.destroy( - new CloudSQLConnectorError({ - code: 'ERRCLOSED', - message: 'The connector was closed.', - }) - ); - } + socket.destroy( + new CloudSQLConnectorError({ + code: 'ERRCLOSED', + message: 'The connector was closed.', + }) + ); } } @@ -393,15 +391,15 @@ export class CloudSQLInstance { } } addSocket(socket: DestroyableSocket) { - // Track all active sockets created by this instance so they can - // be forcefully cleaned up during a domain change or when - // the connector is explicitly closed. - this.sockets.add(socket); + if (!this.instanceInfo.domainName) { + // This was not connected by domain name. Ignore all sockets. + return; + } - // When the socket is closed by the driver or peer, remove it - // from our tracking set to prevent reference memory leaks. - // Note: Node.js TLSSocket/Socket emits 'close', not 'closed'. - socket.once('close', () => { + // Add the socket to the list + this.sockets.add(socket); + // When the socket is closed, remove it. + socket.once('closed', () => { this.sockets.delete(socket); }); } diff --git a/src/connector.ts b/src/connector.ts index 111ad141..37848473 100644 --- a/src/connector.ts +++ b/src/connector.ts @@ -358,17 +358,8 @@ export class Connector { // // Also clear up any local proxy servers and socket connections. close(): void { - for (const entry of this.instances.values()) { - if (entry.isResolved() && entry.instance) { - // If the instance is already resolved, close it synchronously. - // This prevents a race condition with immediate connection pool close - // (e.g., pool.end()) where asynchronous microtasks would execute too late. - entry.instance.close(); - } else { - // Otherwise, close the instance asynchronously once its initial - // refresh has finished. - entry.promise.then(inst => inst.close()).catch(() => {}); - } + for (const instance of this.instances.values()) { + instance.promise.then(inst => inst.close()); } for (const server of this.localProxies) { server.close();