Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(postgres): end a direct pool that finishes opening after its manager closed (#8478)

garrytan9decc28e7d8a
diff --git a/src/core/connection-manager.ts b/src/core/connection-manager.ts
index efcb865e6f..69a570164a 100644
--- a/src/core/connection-manager.ts
+++ b/src/core/connection-manager.ts
@@ -186,6 +186,8 @@ export class ConnectionManager {
/** #1915: single-flight guard for the session-pooler retry. */
private _sessionInit: Promise<Sql> | null = null;
private _sessionFallbackLogged = false;
+ /** #8478: bumped by disconnect() and retire(); a pool whose init began in an earlier epoch has no owner. */
+ private _epoch = 0;
constructor(opts: ConnectionManagerOpts) {
this.opts = opts;
@@ -322,14 +324,12 @@ export class ConnectionManager {
private async getDirectPool(): Promise<Sql> {
if (this._directPool) return this._directPool;
+ const epoch = this._epoch;
// A1: cache the Promise so concurrent first callers await the same init.
if (!this._directInit) {
- this._directInit = this.initDirectPool().then(pool => {
- this._directPool = pool;
- return pool;
- }).catch(err => {
+ this._directInit = this.initDirectPool().then(pool => this.adoptDirectPool(pool, epoch)).catch(err => {
// Reset cache on failure so next caller can retry.
- this._directInit = null;
+ if (this._epoch === epoch) this._directInit = null;
throw err;
});
}
@@ -337,6 +337,8 @@ export class ConnectionManager {
try {
pool = await this._directInit;
} catch (err) {
+ // #8478: a manager closed while this init was in flight starts no fallback.
+ if (this._epoch !== epoch) throw err;
// #1641: the derived direct host (db.<ref>.supabase.co) is IPv6-only
// without Supabase's IPv4 add-on. On IPv4-only networks the direct
// pool can never connect — permanently fall back to the read pool
@@ -365,10 +367,7 @@ export class ConnectionManager {
if (sessionUrl && sessionUrl !== this._directUrl) {
const prevHost = this._directUrl ? this.hostOnly(this._directUrl) : 'unknown host';
this._directUrl = sessionUrl;
- this._sessionInit = this.initDirectPool().then(p => {
- this._directPool = p;
- return p;
- });
+ this._sessionInit = this.initDirectPool().then(p => this.adoptDirectPool(p, epoch));
const p = await this._sessionInit;
if (!this._sessionFallbackLogged) {
this._sessionFallbackLogged = true;
@@ -380,7 +379,8 @@ export class ConnectionManager {
}
return p;
}
- } catch {
+ } catch (sessionErr) {
+ if (this._epoch !== epoch) throw sessionErr;
// Session pooler also failed → fall through to the #1641
// read-pool kill-switch below (the original behavior).
}
@@ -405,6 +405,16 @@ export class ConnectionManager {
return pool;
}
+ /** #8478: a pool that finished opening after disconnect() or retire() is ended here; nobody else holds it. */
+ private async adoptDirectPool(pool: Sql, epoch: number): Promise<Sql> {
+ if (this._epoch !== epoch) {
+ await endPoolBounded(pool);
+ throw Object.assign(new Error('connection-manager: the direct pool finished opening after this manager was closed'), { code: 'CONNECTION_ENDED' });
+ }
+ this._directPool = pool;
+ return pool;
+ }
+
private async initDirectPool(): Promise<Sql> {
if (!this._directUrl) {
throw new Error('connection-manager: cannot init direct pool — no direct URL');
@@ -494,6 +504,8 @@ export class ConnectionManager {
// instance/module pool is added downstream) and neither can hang teardown
// past the CLI's 10s force-exit. endPoolBounded never throws.
const ends: Promise<void>[] = [];
+ this._epoch++;
+ this._sessionInit = null;
if (this._directPool) {
ends.push(endPoolBounded(this._directPool));
this._directPool = null;
@@ -509,6 +521,8 @@ export class ConnectionManager {
/** Ends a manager a reconnect superseded once its in-flight work finishes, with no hard bound (a long hold such as CREATE INDEX CONCURRENTLY completes). */
retire(): void {
const pools = [this._directPool, this._readPoolOwnedExternally ? null : this._readPool];
+ this._epoch++;
+ this._sessionInit = null;
this._directPool = null;
this._directInit = null;
this._readPool = null;
diff --git a/test/connection-manager.serial.test.ts b/test/connection-manager.serial.test.ts
index 7d49a8611b..36d6d9dd12 100644
--- a/test/connection-manager.serial.test.ts
+++ b/test/connection-manager.serial.test.ts
@@ -571,3 +571,98 @@ describe('ConnectionManager — session-pooler retry on unreachable direct host
expect(cm.isKillSwitchActive()).toBe(true);
}, 20000);
});
+
+describe('ConnectionManager — a direct pool that opens after retire()/disconnect() (#8478)', () => {
+ let originalKillSwitch: string | undefined;
+ let originalError: typeof console.error;
+ beforeEach(() => {
+ originalKillSwitch = process.env.GBRAIN_DISABLE_DIRECT_POOL;
+ delete process.env.GBRAIN_DISABLE_DIRECT_POOL;
+ originalError = console.error;
+ console.error = () => {};
+ });
+ afterEach(() => {
+ console.error = originalError;
+ if (originalKillSwitch === undefined) delete process.env.GBRAIN_DISABLE_DIRECT_POOL;
+ else process.env.GBRAIN_DISABLE_DIRECT_POOL = originalKillSwitch;
+ });
+
+ const POOLER_URL = 'postgresql://postgres.abc:[email protected]:6543/db';
+ type Pool = ReturnType<typeof ConnectionManager.prototype.read>;
+ type Internals = { initDirectPool: () => Promise<Pool>; _directPool: Pool | null };
+ const unreachable = () => Object.assign(new Error('getaddrinfo ENOTFOUND db.abc.supabase.co'), { code: 'ENOTFOUND' });
+ const fakePool = () => {
+ const pool = { ends: 0, end: async () => { pool.ends++; } };
+ return pool;
+ };
+
+ for (const shutdown of ['retire', 'disconnect'] as const) {
+ for (const route of ['direct', 'session fallback'] as const) {
+ test(`${shutdown}() while the ${route} pool is opening ends that pool and rejects its waiters`, async () => {
+ const cm = new ConnectionManager({ url: POOLER_URL });
+ cm.setReadPool(fakePool() as unknown as Pool);
+ const late = fakePool();
+ let release!: () => void;
+ const opening = new Promise<void>(res => { release = res; });
+ let attempts = 0;
+ let entered!: () => void;
+ const started = new Promise<void>(res => { entered = res; });
+ (cm as unknown as Internals).initDirectPool = async () => {
+ attempts++;
+ if (route === 'session fallback' && attempts === 1) throw unreachable();
+ entered();
+ await opening;
+ return late as unknown as Pool;
+ };
+ const waiters = Promise.allSettled([cm.ddl(), cm.ddl()]);
+ await started;
+ await cm[shutdown]();
+ release();
+ const outcomes = await waiters;
+ expect(outcomes.map(o => o.status)).toEqual(['rejected', 'rejected']);
+ expect(outcomes.map(o => (o as PromiseRejectedResult).reason?.code)).toEqual(['CONNECTION_ENDED', 'CONNECTION_ENDED']);
+ expect(late.ends).toBe(1);
+ expect((cm as unknown as Internals)._directPool).toBeNull();
+ expect(cm.isKillSwitchActive()).toBe(false);
+ expect(attempts).toBe(route === 'direct' ? 1 : 2);
+ }, 20000);
+ }
+
+ test(`a network failure that lands after ${shutdown}() starts no session retry and trips no kill switch`, async () => {
+ const cm = new ConnectionManager({ url: POOLER_URL });
+ cm.setReadPool(fakePool() as unknown as Pool);
+ let fail!: () => void;
+ const failing = new Promise<void>(res => { fail = res; });
+ let attempts = 0;
+ (cm as unknown as Internals).initDirectPool = async () => {
+ attempts++;
+ await failing;
+ throw unreachable();
+ };
+ const waiters = Promise.allSettled([cm.ddl(), cm.ddl()]);
+ await new Promise(res => setTimeout(res, 5));
+ await cm[shutdown]();
+ fail();
+ expect((await waiters).map(o => o.status)).toEqual(['rejected', 'rejected']);
+ expect(attempts).toBe(1);
+ expect(cm.isKillSwitchActive()).toBe(false);
+ expect(cm.resolveDirectUrl()).toContain('db.abc.supabase.co:5432');
+ }, 20000);
+ }
+
+ test('a manager stays usable after disconnect(): the next ddl() opens and keeps a new direct pool', async () => {
+ const cm = new ConnectionManager({ url: POOLER_URL });
+ cm.setReadPool(fakePool() as unknown as Pool);
+ const first = fakePool();
+ const second = fakePool();
+ const pools = [first, second];
+ (cm as unknown as Internals).initDirectPool = async () => pools.shift() as unknown as Pool;
+ expect(await cm.ddl()).toBe(first as unknown as Pool);
+ await cm.disconnect();
+ expect(first.ends).toBe(1);
+ expect(await cm.ddl()).toBe(second as unknown as Pool);
+ expect(await cm.ddl()).toBe(second as unknown as Pool);
+ expect(second.ends).toBe(0);
+ }, 20000);
+});
+
diff --git a/test/e2e/engine-open-postgres.test.ts b/test/e2e/engine-open-postgres.test.ts
index beebbff45d..7d9e868946 100644
--- a/test/e2e/engine-open-postgres.test.ts
+++ b/test/e2e/engine-open-postgres.test.ts
@@ -208,6 +208,28 @@ describeE2E('openEngine on Postgres', () => {
expect(await codeOf(oldDirect`SELECT 1`)).not.toBe('resolved');
}, 60_000);
+ test('#8478: a direct pool still opening when a reconnect retires its manager is ended, not leaked', async () => {
+ const engine = await openEngine({ embedded_core: true, engine: 'postgres', database: { url: urls[0]!, directUrl: urls[0]! }, pool: { max: 2 }, host: { id: randomUUID() } });
+ const manager = internals(engine).connectionManager as unknown as { ddl(): Promise<postgres.Sql>; initDirectPool(): Promise<postgres.Sql> };
+ const open = manager.initDirectPool.bind(manager);
+ let opened!: (pool: postgres.Sql) => void;
+ const probed = new Promise<postgres.Sql>(resolve => { opened = resolve; });
+ let release!: () => void;
+ const handoff = new Promise<void>(resolve => { release = resolve; });
+ manager.initDirectPool = async () => { const pool = await open(); opened(pool); await handoff; return pool; };
+ const waiting = codeOf(manager.ddl());
+ try {
+ const late = await probed;
+ await engine.reconnect();
+ release();
+ expect(await waiting).toBe('CONNECTION_ENDED');
+ expect(await codeOf(late`SELECT 1`)).not.toBe('resolved');
+ } finally {
+ release();
+ await engine.disconnect();
+ }
+ }, 60_000);
+
test('a reconnect lets in-flight work on the superseded direct pool finish before that pool ends', async () => {
const engine = await openEngine({ embedded_core: true, engine: 'postgres', database: { url: urls[0]!, directUrl: urls[0]! }, pool: { max: 2 }, host: { id: randomUUID() } });
try {