Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(notifier): a SIGTERM hands the commit-notifier lease over at once (GBRA-133 2b-d2)

garrytana27d9baab6c6
diff --git a/src/core/commit-notifier.ts b/src/core/commit-notifier.ts
index fe572b6fcc..3a0dc4057e 100644
--- a/src/core/commit-notifier.ts
+++ b/src/core/commit-notifier.ts
@@ -360,6 +360,7 @@ export function startCommitNotifier(opts: CommitNotifierOptions): CommitNotifier
};
const ownership = async () => {
+ if (stopped) return;
const r = await guard('claiming the notifier lease', () => opts.store.claimLease({ hostId: opts.hostId, ownerId, kind: opts.resident, pid, leaseMs }), undefined);
if (r === undefined) return;
if (r === null) { leased = false; if (!owner) { owner = true; log(`owner of this host's notifier (${opts.resident})`); } return; }
@@ -577,11 +578,15 @@ export function startCommitNotifier(opts: CommitNotifierOptions): CommitNotifier
if (nudgeTimer !== null) clearTimer(nudgeTimer);
closeAllWatches();
}
+ // Hand the lease over before settling: a stop on a termination signal has a short cleanup deadline, and a step
+ // still in flight re-checks the lease before any start (and claims nothing once stopped).
+ const release = () => owner && leased ? guard('releasing the notifier lease', () => opts.store.releaseLease(opts.hostId, ownerId), undefined) : undefined;
+ await release();
if (inflight) {
await settleWithin(inflight, STOP_SETTLE_MS);
closeAllWatches();
+ await release();
}
- if (owner && leased) await guard('releasing the notifier lease', () => opts.store.releaseLease(opts.hostId, ownerId), undefined);
},
};
}
diff --git a/src/core/notifier/resident.ts b/src/core/notifier/resident.ts
index 7c970df902..0754b76787 100644
--- a/src/core/notifier/resident.ts
+++ b/src/core/notifier/resident.ts
@@ -14,6 +14,7 @@ import { appendFileSync, mkdirSync, statSync, writeFileSync, readFileSync } from
import { dirname } from 'node:path';
import type { BrainEngine } from '../engine.ts';
import { gbrainPath } from '../config.ts';
+import { registerCleanup } from '../process-cleanup.ts';
import { SYNC_ON_COMMIT_KEY } from '../sync-config-keys.ts';
import { localHostId } from '../persistence/identity.ts';
import {
@@ -185,9 +186,14 @@ export function startResidentNotifier(engine: BrainEngine, opts: ResidentNotifie
const entry = { notifier, resident: opts.resident };
active = entry;
const stop = notifier.stop;
+ // GBRA-133 2b-d2: a termination signal the process does not own (SIGTERM/SIGHUP on serve, serve --http, the
+ // autopilot daemon) runs the cleanup pass and exits; stopping here releases the host's notifier lease first, so the
+ // next resident owns it at its first claim instead of after the lease expires. A SIGKILL still waits for expiry.
+ let deregisterCleanup = (): void => {};
const wrapped: CommitNotifier = Object.assign(Object.create(Object.getPrototypeOf(notifier)), notifier, {
- stop: async () => { if (active === entry) active = null; await stop(); },
+ stop: async () => { deregisterCleanup(); if (active === entry) active = null; await stop(); },
});
+ deregisterCleanup = registerCleanup(`commit-notifier:${opts.resident}`, () => wrapped.stop());
Object.defineProperties(wrapped, {
enabled: { get: () => notifier.enabled }, owner: { get: () => notifier.owner }, decisions: { get: () => notifier.decisions },
});
diff --git a/test/e2e/s-notifier-sigterm-lease-postgres.test.ts b/test/e2e/s-notifier-sigterm-lease-postgres.test.ts
new file mode 100644
index 0000000000..89d70046dd
--- /dev/null
+++ b/test/e2e/s-notifier-sigterm-lease-postgres.test.ts
@@ -0,0 +1,3 @@
+import { registerPostgresTests } from '../helpers/test-backends.ts';
+
+await registerPostgresTests(() => import('../pathological/s-notifier-sigterm-lease.test.ts'));
diff --git a/test/pathological/s-notifier-sigterm-lease.test.ts b/test/pathological/s-notifier-sigterm-lease.test.ts
new file mode 100644
index 0000000000..95dc93068e
--- /dev/null
+++ b/test/pathological/s-notifier-sigterm-lease.test.ts
@@ -0,0 +1,137 @@
+/**
+ * GBRA-133 2b-d2: a resident stopped by SIGTERM hands the host's commit notifier over at once.
+ *
+ * Before: stdio `gbrain serve` on SIGTERM ran the generic cleanup pass and exited 143 while its own graceful shutdown
+ * (which stops the notifier and releases the lease) was still starting, so the Postgres lease row stayed live until it
+ * expired (two heartbeats, 20 s by default) and the next resident neither owned the notifier nor synced a commit until
+ * then. Now the notifier registers a cleanup that releases the lease first. A SIGKILL still waits for expiry; lease
+ * TTLs are unchanged.
+ *
+ * Protects (real `gbrain serve` processes): after a SIGTERM the lease is released (Postgres; PGLite has no lease row,
+ * its lock holder owns), and a second resident owns the notifier and syncs a new commit while the first resident's
+ * lease, stretched to 120 s here, would still be live. PGLite always, Postgres when DATABASE_URL is set (e2e twin).
+ */
+import { describe, expect, test } from 'bun:test';
+import { execFileSync } from 'node:child_process';
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join, resolve } from 'node:path';
+import type { BrainEngine } from '../../src/core/engine.ts';
+import { PGLiteEngine } from '../../src/core/pglite-engine.ts';
+import { PostgresEngine } from '../../src/core/postgres-engine.ts';
+import { performSync } from '../../src/commands/sync.ts';
+import { isolatedPersistencePostgres } from '../helpers/persistence-postgres.ts';
+import { withEnv } from '../helpers/with-env.ts';
+import { pathologicalArms } from './harness.ts';
+import { record } from './s-blockers-fixture.ts';
+
+const CLI = resolve(import.meta.dir, '../../src/cli.ts');
+const SOURCE = 'leasecase';
+const HEARTBEAT_MS = 60_000;
+const md = (title: string) => '---\ntitle: ' + title + '\ntype: note\n---\nThe ' + title + ' note.\n';
+
+interface Resident { pid: number; spawnedAt: number; lines: Array<{ at: number; text: string }>; exited: Promise<number>; kill(signal: NodeJS.Signals): void }
+
+function startServe(env: Record<string, string>): Resident {
+ const spawnedAt = performance.now();
+ const child = Bun.spawn([process.execPath, CLI, 'serve'], { env, stdin: 'pipe', stdout: 'pipe', stderr: 'pipe' });
+ const lines: Resident['lines'] = [];
+ void (async () => {
+ let buf = '';
+ for await (const chunk of child.stderr) {
+ buf += new TextDecoder().decode(chunk);
+ let i: number;
+ while ((i = buf.indexOf('\n')) >= 0) { lines.push({ at: performance.now(), text: buf.slice(0, i) }); buf = buf.slice(i + 1); }
+ }
+ })();
+ return { pid: child.pid, spawnedAt, lines, exited: child.exited, kill: signal => child.kill(signal) };
+}
+
+async function waitLine(r: Resident, pattern: RegExp, timeoutMs: number, after = 0): Promise<number | null> {
+ const deadline = performance.now() + timeoutMs;
+ for (;;) {
+ const hit = r.lines.find(line => line.at >= after && pattern.test(line.text));
+ if (hit) return hit.at;
+ if (performance.now() > deadline) return null;
+ await Bun.sleep(50);
+ }
+}
+
+describe('GBRA-133 2b-d2: SIGTERM releases the commit notifier lease', () => {
+ for (const { backend, databaseUrl } of pathologicalArms()) {
+ test(`a second resident owns the notifier and syncs a commit right after the first gets SIGTERM (${backend})`, async () => {
+ const home = mkdtempSync(join(tmpdir(), 'gbrain-sigterm-lease-'));
+ const repo = join(home, 'repo');
+ const git = (...args: string[]) => execFileSync('git', ['-C', repo, '-c', 'user.name=Example', '-c', '[email protected]', ...args], { encoding: 'utf8' }).trim();
+ const pg = databaseUrl ? await isolatedPersistencePostgres(databaseUrl) : null;
+ const dbConfig = pg ? { engine: 'postgres', database_url: pg.databaseUrl } : { engine: 'pglite', database_path: join(home, 'brain.pglite') };
+ const open = async (): Promise<BrainEngine> => {
+ const engine = pg ? new PostgresEngine() : new PGLiteEngine();
+ await engine.connect(pg ? { database_url: pg.databaseUrl } : { database_path: join(home, 'brain.pglite') });
+ return engine;
+ };
+ const residents: Resident[] = [];
+ try {
+ if (pg) await pg.engine.disconnect();
+ mkdirSync(join(home, '.gbrain'), { recursive: true });
+ writeFileSync(join(home, '.gbrain/config.json'), JSON.stringify(dbConfig));
+ mkdirSync(join(repo, 'notes'), { recursive: true });
+ git('init', '-q', '-b', 'main');
+ writeFileSync(join(repo, 'notes/first.md'), md('first'));
+ git('add', '-A'); git('commit', '-qm', 'first');
+ const env: Record<string, string> = { ...process.env as Record<string, string>, GBRAIN_HOME: home, GBRAIN_NOTIFIER_CLASSIC_SOURCES: SOURCE,
+ GBRAIN_NOTIFIER_HEARTBEAT_MS: String(HEARTBEAT_MS), GBRAIN_NOTIFIER_POLL_MS: '1000', GBRAIN_NOTIFIER_QUIET_MS: '0' };
+ for (const key of ['DATABASE_URL', 'GBRAIN_DATABASE_URL', 'OPENAI_API_KEY', 'ANTHROPIC_API_KEY', 'VOYAGE_API_KEY', 'GEMINI_API_KEY']) delete env[key];
+
+ await withEnv({ GBRAIN_HOME: home, DATABASE_URL: undefined, GBRAIN_DATABASE_URL: undefined }, async () => {
+ const engine = await open();
+ try {
+ await engine.initSchema();
+ await engine.executeRaw("INSERT INTO sources(id,name,local_path,config) VALUES($1,$1,$2,'{}')", [SOURCE, repo]);
+ await engine.setConfig('sync.on_commit', 'true');
+ await performSync(engine, { sourceId: SOURCE, repoPath: repo, noPull: true, noEmbed: true, noExtract: true });
+ } finally { await engine.disconnect(); }
+ });
+
+ const first = startServe(env); residents.push(first);
+ expect(await waitLine(first, /owner of this host's notifier/, 60_000)).not.toBeNull();
+ first.kill('SIGTERM');
+ await first.exited;
+
+ const lease = await withEnv({ GBRAIN_HOME: home, DATABASE_URL: undefined, GBRAIN_DATABASE_URL: undefined }, async () => {
+ const engine = await open();
+ try { return await engine.executeRaw<{ pid: number; live: boolean }>('SELECT pid, expires_at > now() AS live FROM sync_notifier_leases'); }
+ finally { await engine.disconnect(); }
+ });
+ // Postgres: the SIGTERMed owner's row is released. PGLite writes no lease row (the lock holder owns).
+ expect(lease.map(row => ({ pid: Number(row.pid), live: row.live }))).toEqual(pg ? [{ pid: first.pid, live: false }] : []);
+
+ const second = startServe(env); residents.push(second);
+ // The first resident's lease (two 60 s heartbeats) would still be live for two minutes; owning within one
+ // means the SIGTERM handed it over.
+ const ownedAt = await waitLine(second, /owner of this host's notifier/, 60_000);
+ expect(ownedAt).not.toBeNull();
+ writeFileSync(join(repo, 'notes/second.md'), md('second'));
+ git('add', '-A'); git('commit', '-qm', 'second');
+ const committedAt = performance.now();
+ const syncedAt = await waitLine(second, /done job=\S+ status=synced/, 60_000, committedAt);
+ record({ case: '2b-d2-sigterm-lease', backend, owner_ms: ownedAt === null ? null : Math.round(ownedAt - second.spawnedAt),
+ commit_to_synced_ms: syncedAt === null ? null : Math.round(syncedAt - committedAt) });
+ expect(syncedAt).not.toBeNull();
+ second.kill('SIGTERM');
+ await second.exited;
+
+ const page = await withEnv({ GBRAIN_HOME: home, DATABASE_URL: undefined, GBRAIN_DATABASE_URL: undefined }, async () => {
+ const engine = await open();
+ try { return await engine.executeRaw<{ slug: string }>("SELECT slug FROM pages WHERE source_id=$1 AND slug='notes/second' AND deleted_at IS NULL", [SOURCE]); }
+ finally { await engine.disconnect(); }
+ });
+ expect(page).toHaveLength(1);
+ } finally {
+ for (const r of residents) { try { r.kill('SIGKILL'); } catch { /* exited */ } await r.exited.catch(() => undefined); }
+ if (pg) await pg.close().catch(() => undefined);
+ rmSync(home, { recursive: true, force: true });
+ }
+ }, 240_000);
+ }
+});