Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(db): keep the pool shutdown guard timer referenced (#5435)
time-attack583f60ce6091
diff --git a/src/core/db.ts b/src/core/db.tsindex 53dbc4ba56..72b4c9ba45 100644--- a/src/core/db.ts+++ b/src/core/db.ts@@ -29,6 +29,10 @@ export const POOL_END_TIMEOUT_SECONDS = 2;* rejects is worse than one that races past a stuck socket. The race timer is* the real guarantee; `{ timeout }` just lets a healthy drain return in ms.*+ * The guard timer stays referenced: an unref'd guard is not serviced under+ * `bun test` on Windows, which disarms the bound. The finally clearTimeout keeps+ * the fast path from holding the event loop.+ ** Note callers that close MULTIPLE pools should `Promise.all` them rather than* awaiting sequentially, so the per-pool bounds run concurrently instead of* stacking.@@ -39,7 +43,6 @@ export async function endPoolBounded(let timer: ReturnType<typeof setTimeout> | undefined;const guard = new Promise<void>((resolve) => {timer = setTimeout(resolve, POOL_END_TIMEOUT_SECONDS * 1000 + 500);- timer.unref?.();});try {await Promise.race([diff --git a/test/postgres-disconnect-bounded.test.ts b/test/postgres-disconnect-bounded.test.tsindex 68dea784fa..37904e3020 100644--- a/test/postgres-disconnect-bounded.test.ts+++ b/test/postgres-disconnect-bounded.test.ts@@ -10,6 +10,8 @@*/import { describe, test, expect } from 'bun:test';+import { spawnSync } from 'child_process';+import { join } from 'path';import { endPoolBounded, POOL_END_TIMEOUT_SECONDS } from '../src/core/db.ts';describe('endPoolBounded', () => {@@ -37,4 +39,34 @@ describe('endPoolBounded', () => {const pool = { end: async () => { throw new Error('pool boom'); } };await expect(endPoolBounded(pool)).resolves.toBeUndefined();});++ // #5332 platform coverage. The Windows `bun test` scheduling that skips an+ // unref'd timer cannot run on CI, so this pins the mechanism: the guard+ // timer is never unref'd.+ test('the guard timer is never unref\'d', async () => {+ const original = globalThis.setTimeout;+ let unrefCalls = 0;+ globalThis.setTimeout = ((fn: () => void, ms?: number) => {+ const t = original(fn, ms);+ const origUnref = (t as { unref?: () => void }).unref;+ if (origUnref) (t as { unref: () => void }).unref = () => { unrefCalls += 1; return origUnref.call(t); };+ return t;+ }) as typeof setTimeout;+ try {+ await endPoolBounded({ end: () => new Promise<void>(() => { /* never resolves */ }) });+ } finally {+ globalThis.setTimeout = original;+ }+ expect(unrefCalls).toBe(0);+ });++ test('a process with nothing else on its loop still returns from a never-settling end', () => {+ const db = join(import.meta.dir, '../src/core/db.ts');+ const child = spawnSync(process.execPath, ['--no-env-file', '--eval', `+ const { endPoolBounded } = await import(${JSON.stringify(db)});+ await endPoolBounded({ end: () => new Promise(() => {}) });+ console.log('bounded');+ `], { encoding: 'utf-8', timeout: 15_000 });+ expect({ status: child.status, stdout: child.stdout.trim() }).toEqual({ status: 0, stdout: 'bounded' });+ }, 20_000);});