mirror of
https://github.com/garrytan/gbrain.git
synced 2026-07-27 22:15:33 +00:00
* fix(jobs): reap stale dead-holder cycle/sync locks (#1972) A crashed sync (OOM, recycle, SIGKILL) stranded its gbrain_cycle_locks row until something contended for it — reclaim was on-contention only. Add a host-scoped background reaper: reapDeadHolderLocks deletes locks whose holder PID is provably dead on this host, scoped to the gbrain-sync:*/gbrain-cycle* namespaces only (never elections/supervisor/reindex), with a snapshot-matched delete (date_trunc on acquired_at) that is TOCTOU-safe against PID reuse. Reuses isHolderDeadLocally (same-host + ESRCH + 60s grace). doctor --fix now auto-reaps for no-autopilot brains. DRY: selectLockRows + shared mapper now back inspectLock + listStaleLocks (killed the triplication). * fix(db): bound pool disconnect so teardown can't eat CLI output (#1972) pool.end() against PgBouncer transaction-mode never drained, so disconnect blocked until the CLI's 10s force-exit fired and process.exit()'d mid-write, truncating stdout (e.g. #1959's relational query returned empty). Add a gbrain-owned endPoolBounded(pool): Promise.race of pool.end({timeout}) against a hard timer, so teardown is bounded regardless of what postgres.js does and is testable. connection-manager ends its direct + read pools concurrently so the per-pool bounds don't stack. PGLite disconnect is unaffected. * fix(cycle): complete cooperative-abort coverage + wire lock reaper (#1972) v0.42.29 made only the embed phase honor the abort signal; a 24h pull still showed force-evicts from a long non-embed phase ignoring it. Thread the signal into every cycle-reachable long loop: extract (extractForSlugs + the full-walk extractLinksFromDir/extractTimelineFromDir), extract_facts (per-page loop + embed signal + the phantom-redirect 30s lock-retry), and consolidate's bucket loop. Add a terminal abort check so an aborted cycle never stamps last_full_cycle_at as a completed run (Codex #9). lint now yields + checks abort every 200 pages (it's synchronous; the yield is what lets the signal land). New phase-duration force-evict attribution log names any phase that crosses the 30s deadline. Wire reapDeadHolderLocks at cycle start. * chore: bump version and changelog (v0.42.38.0) #1972 — stale-lock reaper, bounded pool disconnect, and complete cooperative-abort coverage across cycle phases. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs(key-files): current-state for the #1972 reaper, bounded disconnect, abort coverage document-release: update db-lock.ts (reapDeadHolderLocks + selectLockRows DRY), db.ts (endPoolBounded), and abort-check.ts (coverage now spans extract/ extract_facts/consolidate/lint + terminal guard) entries to current truth. * test(isolation): fix shard-order flakes exposed by #1972's new test files Adding 3 new test files reshuffled the hash-based shards, exposing two pre-existing test-isolation bugs: - cycle-consolidate.test.ts assumed the global legacy-embedding preload's 1536-d gateway config still held at initSchema, but a co-sharded test that calls resetGateway() in teardown nulls it, so initSchema fell back to the 1280-d default and built a halfvec(1280) facts column its 1536-d fixtures can't fill. Re-pin the legacy OpenAI/1536 config in beforeAll (the pattern legacy-embedding-preload.ts documents for 1536-d fixture tests). - db-lock-heartbeat-takeover.test.ts (merged from master's #1794) mutated process.env.GBRAIN_LOCK_STEAL_GRACE_SECONDS raw, tripping check:test-isolation rule R1. Convert to withEnv(). --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
163 lines
6.7 KiB
TypeScript
163 lines
6.7 KiB
TypeScript
/**
|
|
* #1972 — host-scoped reaper for dead-holder sync/cycle locks.
|
|
*
|
|
* Covers:
|
|
* - reapDeadHolderLocks: namespace scope (sync/cycle only, NOT election/etc),
|
|
* same-host dead-PID reaped regardless of TTL, live/cross-host/within-grace
|
|
* kept. Uses the injectable process.kill seam so it's deterministic.
|
|
* - deleteLockRowExact: snapshot-matched delete (the TOCTOU defense) — a
|
|
* non-matching acquired_at is a no-op, the matching one deletes.
|
|
*/
|
|
|
|
import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';
|
|
import { hostname } from 'os';
|
|
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
|
|
import {
|
|
reapDeadHolderLocks,
|
|
deleteLockRowExact,
|
|
inspectLock,
|
|
HOLDER_TAKEOVER_GRACE_MS,
|
|
type HolderLivenessOpts,
|
|
} from '../src/core/db-lock.ts';
|
|
|
|
let engine: PGLiteEngine;
|
|
|
|
beforeAll(async () => {
|
|
engine = new PGLiteEngine();
|
|
await engine.connect({ database_url: '' });
|
|
await engine.initSchema();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await engine.disconnect();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await engine.executeRaw(`DELETE FROM gbrain_cycle_locks`);
|
|
});
|
|
|
|
const LOCAL = hostname();
|
|
const OLD_S = Math.ceil(HOLDER_TAKEOVER_GRACE_MS / 1000) + 60; // comfortably past grace
|
|
const YOUNG_S = 5;
|
|
|
|
/** Insert a lock row with a given age + namespace. ttlFuture controls whether
|
|
* the row is TTL-expired (to prove the reaper reaps dead holders regardless). */
|
|
async function seedLock(
|
|
id: string,
|
|
holderPid: number,
|
|
holderHost: string,
|
|
ageSeconds: number,
|
|
ttlFuture = true,
|
|
) {
|
|
const ttl = ttlFuture ? `NOW() + INTERVAL '10 minutes'` : `NOW() - INTERVAL '1 minute'`;
|
|
await engine.executeRaw(
|
|
`INSERT INTO gbrain_cycle_locks (id, holder_pid, holder_host, acquired_at, ttl_expires_at, last_refreshed_at)
|
|
VALUES ($1, $2, $3, NOW() - ($4 || ' seconds')::interval, ${ttl}, NOW() - ($4 || ' seconds')::interval)`,
|
|
[id, holderPid, holderHost, String(ageSeconds)],
|
|
);
|
|
}
|
|
|
|
/** process.kill seam: every pid is dead (ESRCH) except those in `live`. */
|
|
function killSeam(live: Set<number>): HolderLivenessOpts {
|
|
return {
|
|
localHost: LOCAL,
|
|
processKill: (pid: number) => {
|
|
if (live.has(pid)) return; // alive
|
|
const e = new Error('no such process') as NodeJS.ErrnoException;
|
|
e.code = 'ESRCH';
|
|
throw e;
|
|
},
|
|
};
|
|
}
|
|
|
|
async function lockIds(): Promise<string[]> {
|
|
const rows = await engine.executeRaw<{ id: string }>(`SELECT id FROM gbrain_cycle_locks ORDER BY id`);
|
|
return rows.map(r => r.id);
|
|
}
|
|
|
|
describe('reapDeadHolderLocks', () => {
|
|
test('reaps same-host dead-PID sync + cycle locks (regardless of TTL)', async () => {
|
|
await seedLock('gbrain-sync:src-a', 900001, LOCAL, OLD_S, /*ttlFuture*/ true); // dead, TTL NOT expired
|
|
await seedLock('gbrain-cycle', 900002, LOCAL, OLD_S, false); // dead, TTL expired
|
|
await seedLock('gbrain-cycle:src-b', 900003, LOCAL, OLD_S, true); // dead, TTL NOT expired
|
|
|
|
const { reaped, reapedIds } = await reapDeadHolderLocks(engine, killSeam(new Set()));
|
|
expect(reaped).toBe(3);
|
|
expect(reapedIds.sort()).toEqual(['gbrain-cycle', 'gbrain-cycle:src-b', 'gbrain-sync:src-a']);
|
|
expect(await lockIds()).toEqual([]);
|
|
});
|
|
|
|
test('keeps a live same-host holder', async () => {
|
|
await seedLock('gbrain-sync:src-live', process.pid, LOCAL, OLD_S);
|
|
const { reaped } = await reapDeadHolderLocks(engine, killSeam(new Set([process.pid])));
|
|
expect(reaped).toBe(0);
|
|
expect(await lockIds()).toEqual(['gbrain-sync:src-live']);
|
|
});
|
|
|
|
test('keeps a dead holder still within the PID-reuse grace window', async () => {
|
|
await seedLock('gbrain-sync:src-young', 900010, LOCAL, YOUNG_S);
|
|
const { reaped } = await reapDeadHolderLocks(engine, killSeam(new Set()));
|
|
expect(reaped).toBe(0);
|
|
expect(await lockIds()).toEqual(['gbrain-sync:src-young']);
|
|
});
|
|
|
|
test('keeps a cross-host holder (cannot probe a remote PID)', async () => {
|
|
await seedLock('gbrain-sync:src-xhost', 900011, 'a-different-host', OLD_S);
|
|
const { reaped } = await reapDeadHolderLocks(engine, killSeam(new Set()));
|
|
expect(reaped).toBe(0);
|
|
expect(await lockIds()).toEqual(['gbrain-sync:src-xhost']);
|
|
});
|
|
|
|
test('NEVER reaps a non-sync/cycle namespace, even with a dead PID (blast radius)', async () => {
|
|
await seedLock('gbrain-election:leader', 900020, LOCAL, OLD_S);
|
|
await seedLock('some-other-lock', 900021, LOCAL, OLD_S);
|
|
const { reaped } = await reapDeadHolderLocks(engine, killSeam(new Set()));
|
|
expect(reaped).toBe(0);
|
|
expect(await lockIds()).toEqual(['gbrain-election:leader', 'some-other-lock']);
|
|
});
|
|
|
|
test('empty table → {reaped:0}', async () => {
|
|
const r = await reapDeadHolderLocks(engine, killSeam(new Set()));
|
|
expect(r).toEqual({ reaped: 0, reapedIds: [] });
|
|
});
|
|
|
|
test('mixed set: reaps only the eligible rows', async () => {
|
|
await seedLock('gbrain-sync:dead', 900030, LOCAL, OLD_S); // reap
|
|
await seedLock('gbrain-sync:live', process.pid, LOCAL, OLD_S); // keep (live)
|
|
await seedLock('gbrain-cycle:xhost', 900031, 'other', OLD_S); // keep (cross-host)
|
|
await seedLock('gbrain-election:x', 900032, LOCAL, OLD_S); // keep (namespace)
|
|
const { reaped, reapedIds } = await reapDeadHolderLocks(engine, killSeam(new Set([process.pid])));
|
|
expect(reaped).toBe(1);
|
|
expect(reapedIds).toEqual(['gbrain-sync:dead']);
|
|
expect(await lockIds()).toEqual(['gbrain-cycle:xhost', 'gbrain-election:x', 'gbrain-sync:live']);
|
|
});
|
|
});
|
|
|
|
describe('deleteLockRowExact (snapshot-matched, TOCTOU defense)', () => {
|
|
test('no-op when acquired_at does not match; deletes when it does', async () => {
|
|
await seedLock('gbrain-sync:exact', 900040, LOCAL, OLD_S);
|
|
const snap = await inspectLock(engine, 'gbrain-sync:exact');
|
|
expect(snap).not.toBeNull();
|
|
|
|
// Simulate a reused-PID takeover: same id + pid, but a different (newer)
|
|
// acquired_at than the one the reaper snapshotted → must NOT delete.
|
|
const wrong = new Date(snap!.acquired_at.getTime() + 3_600_000);
|
|
const miss = await deleteLockRowExact(engine, 'gbrain-sync:exact', 900040, wrong);
|
|
expect(miss.deleted).toBe(false);
|
|
expect(await lockIds()).toEqual(['gbrain-sync:exact']);
|
|
|
|
// The matching snapshot deletes.
|
|
const hit = await deleteLockRowExact(engine, 'gbrain-sync:exact', 900040, snap!.acquired_at);
|
|
expect(hit.deleted).toBe(true);
|
|
expect(await lockIds()).toEqual([]);
|
|
});
|
|
|
|
test('no-op when holder_pid does not match', async () => {
|
|
await seedLock('gbrain-sync:pidguard', 900050, LOCAL, OLD_S);
|
|
const snap = await inspectLock(engine, 'gbrain-sync:pidguard');
|
|
const res = await deleteLockRowExact(engine, 'gbrain-sync:pidguard', 111111, snap!.acquired_at);
|
|
expect(res.deleted).toBe(false);
|
|
expect(await lockIds()).toEqual(['gbrain-sync:pidguard']);
|
|
});
|
|
});
|