diff --git a/pg/lib/client.ts b/pg/lib/client.ts index 4517e1659..264143b18 100644 --- a/pg/lib/client.ts +++ b/pg/lib/client.ts @@ -358,7 +358,11 @@ class AwsPGPooledConnection extends BaseAwsPgClient { throw new UndefinedClientError(); } this.pluginService.removeErrorListener(this.targetClient); - return await this.targetClient.client.release(); + // After failover, the new target client may not have a release() method + // (e.g., a direct connection instead of a pooled connection). + if (typeof this.targetClient.client?.release === "function") { + return await this.targetClient.client.release(); + } }, null ); diff --git a/pg/lib/icp/pg_internal_pool_client.ts b/pg/lib/icp/pg_internal_pool_client.ts index ed0324db1..e7d6c6128 100644 --- a/pg/lib/icp/pg_internal_pool_client.ts +++ b/pg/lib/icp/pg_internal_pool_client.ts @@ -25,6 +25,12 @@ export class AwsPgInternalPoolClient implements AwsInternalPoolClient { constructor(props: pkgPg.PoolConfig) { this.targetPool = new pkgPg.Pool(props); + // Handle idle client errors to prevent process crash during Aurora failover. + // When a failover occurs, idle connections in the pool are terminated by the server, + // which emits 'error' events. Without this handler, Node.js throws an uncaught exception. + this.targetPool.on("error", () => { + // Intentionally swallowed. The failover plugin will handle reconnection. + }); } async end(): Promise {