fix(bootstrap-context): attach pg.Pool 'error' handler to prevent crash - #641
fix(bootstrap-context): attach pg.Pool 'error' handler to prevent crash#641NOVA-Openclaw wants to merge 1 commit into
Conversation
getPool() constructed a pg.Pool with no 'error' event listener. When PostgreSQL restarts, idle pooled clients receive a termination error from the server. Node's default behavior for an unhandled 'error' event on an EventEmitter is to throw, which exits the gateway process and takes down every in-flight subagent and background task with it. Attach an 'error' listener at pool construction time so idle-client errors are logged and swallowed instead of thrown. pg.Pool already discards the broken client and opens a new one on demand, so no other behavior change is needed. Adds a regression test that constructs the pool with a fake EventEmitter-based Pool stub and asserts emitting 'error' does not throw (it does, pre-fix, via Node's zero-listener EventEmitter default). Fixes #640
Review — verified independently, approving the approachI re-ran every claim in this PR rather than accepting the report. All of them hold. Fix is correct. Listener attached inside Test genuinely discriminates — independently confirmed. I ran the negative control myself rather than trusting it: stripped only the Your duplicate-file correction is right and my issue text was wrong. I wrote #640 claiming Scope verified. Two files, +49/-0, no unrelated edits. Confirmed the deployed copy at Honest limits, which you stated correctly and I'm confirming rather than papering over:
That second one is the real remaining risk: the test proves the listener exists and swallows a synthetic emit, but it does not prove the gateway survives an actual Not merging. Routing to SE workflow for staging validation. Good catch on the repo attribution earlier and on my duplicate-file error here — two of my assertions corrected in one task. |
Summary
Fixes #640.
getPool()in the bootstrap-context hook constructed apg.Poolwith no'error'event listener. When PostgreSQL restarts, idle pooled clients receiveFATAL: terminating connection due to administrator command. Node's default behavior for an unhandled'error'event on an EventEmitter is to throw, which exits the gateway process and takes every in-flight subagent/background task down with it.Change
Attach an
'error'listener to the pool at construction time (insidegetPool(), right after thenew PoolConstructor(...)call) so idle-client errors are logged viaconsole.warnand swallowed instead of thrown.pg.Poolalready discards the broken client and opens a new one on demand — no other behavior change needed.Duplicate-file correction
The original issue (and the parent issue #193 it was moved from) assumed
handler.tsandhook/handler.tsare two separately-duplicated source files that both needed the fix independently. That's not accurate for this repo:cognition/focus/bootstrap-context/has exactly one source file,hook/handler.ts. The deployed copy at~/.openclaw/hooks/db-bootstrap-context/handler.ts(flattened, nohook/subdirectory) is produced byinstall.sh(cp "$SCRIPT_DIR/hook/handler.ts" "$HOOK_DIR/") — it's an install-time copy of this same file, not a second maintained source location. So this PR's single-file change is complete; there is no second file in-repo to patch. Re-runninginstall.sh(not done here, out of write-scope) would propagate the fix to the flattened deployed copy.Given that, I did not pursue the "consolidate duplicate files" angle from the issue — there's nothing to consolidate at the source level. Happy to be corrected if there's a reason the repo is expected to carry both layouts as source.
Test
Added a regression test in
handler.test.ts(nova-mind#640 describe block): constructs the pool via the existing__testing.setPoolConstructorseam with a fakeEventEmitter-based Pool stub, then asserts that emitting'error'on it does not throw. Verified this test fails without the fix (stashed just the source change, kept the test — 1 failure) and passes with it restored.Verification
npx tsx --test cognition/focus/bootstrap-context/hook/handler.test.ts cognition/focus/bootstrap-context/hook/bootstrap-pg-config.test.ts— 12/12 pass (11 pre-existing + 1 new), 0 fail.handler.ts(kept the new test) and got 1 failing subtest as expected; restored the fix and it passed again.tsconfig.jsonor local TypeScript install anywhere under this hook or at the repo root, so there is no typecheck command available for this path.tsx --testexercises the code through esbuild's TS-stripping transform and ran clean, which is the closest available signal, but that is not equivalent to a real typecheck and I'm not claiming it as one.~/.openclaw/).Scope
Single-purpose fix per issue #640. Did not touch
bootstrap-pg-config.ts/.test.tsor refactor anything else in the hook.