Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(minions): only a requested shutdown hands undrained claims back; a cooperative stop() keeps them
time-attack75130fbde336
diff --git a/src/core/minions/worker-shutdown.ts b/src/core/minions/worker-shutdown.tsindex 984da52896..966fc2c1b7 100644--- a/src/core/minions/worker-shutdown.ts+++ b/src/core/minions/worker-shutdown.ts@@ -43,7 +43,13 @@ export async function settleShutdownInterruptedJob(}}-/** After the drain window: hand back every claim still held. Returns true when any was. */+/**+ * After a shutdown's drain window: hand back every claim still held. Returns+ * true when any was. Called only when shutdown was requested (the process is+ * about to exit); a cooperative `stop()` keeps its claims, whose handlers are+ * still running in this process. An isolated child whose termination was not+ * confirmed is not passed in: the stall sweep owns that claim.+ */export async function releaseUndrainedClaims(engine: BrainEngine, executions: Iterable<{ job: MinionJob; lockToken: string }>,): Promise<boolean> {diff --git a/src/core/minions/worker.ts b/src/core/minions/worker.tsindex e366ff0117..7766c4370e 100644--- a/src/core/minions/worker.ts+++ b/src/core/minions/worker.ts@@ -944,7 +944,10 @@ export class MinionWorker extends EventEmitter {new Promise(resolve => setTimeout(resolve, 30000)),]);if (this.configurationError) await this.drainConfiguration();- else this.drainForced = await releaseUndrainedClaims(this.engine, this.executions.values());+ else if (this.shutdownAbort.signal.aborted) {+ const isolated = this.opts.jobIsolation === 'process';+ this.drainForced = await releaseUndrainedClaims(this.engine, [...this.executions.values()].filter(e => !isolated || e.stopped));+ }}// The worker does NOT disconnect the engine: it doesn't own thediff --git a/test/worker-stop-keeps-claim.slow.test.ts b/test/worker-stop-keeps-claim.slow.test.tsnew file mode 100644index 0000000000..3eefe8e9a6--- /dev/null+++ b/test/worker-stop-keeps-claim.slow.test.ts@@ -0,0 +1,54 @@+/**+ * #5062 follow-up: the 30 s shutdown drain hands still-running claims back to+ * the queue only when shutdown was requested (signal, `jobs work` signal+ * owner, watchdog), because the process is about to exit. A cooperative+ * `worker.stop()` (inline remediation, `jobs submit --follow`) keeps the+ * process and the handler alive, so its claim must stay `active` and the job+ * must complete once, not be re-queued and run again.+ * Slow lane: each case waits out the real 30 s drain; both run concurrently.+ */+import { expect, test } from 'bun:test';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { MinionQueue } from '../src/core/minions/queue.ts';+import { MinionWorker } from '../src/core/minions/worker.ts';++async function runCase(mode: 'stop' | 'shutdown') {+ const engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+ try {+ const queue = new MinionQueue(engine);+ const worker = new MinionWorker(engine, { pollInterval: 25, lockDuration: 60_000, healthCheckInterval: 0 });+ let finish!: () => void;+ const gate = new Promise<void>(resolve => { finish = resolve; });+ let runs = 0;+ worker.register('slow-probe', async () => { runs++; await gate; return 'ran'; });+ const job = await queue.add('slow-probe', {}, {}, { allowProtectedSubmit: true });+ const running = worker.start();+ for (let i = 0; i < 400 && (await queue.getJob(job.id))?.status !== 'active'; i++) await new Promise(r => setTimeout(r, 25));+ if (mode === 'stop') worker.stop();+ else worker.requestShutdown();+ await running;+ const afterDrain = await queue.getJob(job.id);+ finish();+ let final = afterDrain;+ for (let i = 0; i < 200 && mode === 'stop' && final?.status === 'active'; i++) {+ await new Promise(r => setTimeout(r, 25));+ final = await queue.getJob(job.id);+ }+ return { afterDrain, final, drainForced: worker.drainForced, runs };+ } finally {+ await engine.disconnect();+ }+}++test('stop() keeps a running claim through the drain and the job completes once; requestShutdown() hands it back', async () => {+ const [stopped, shutdown] = await Promise.all([runCase('stop'), runCase('shutdown')]);+ expect(stopped.afterDrain?.status).toBe('active');+ expect(stopped.drainForced).toBe(false);+ expect(stopped.final?.status).toBe('completed');+ expect(stopped.runs).toBe(1);+ expect(shutdown.afterDrain?.status).toBe('waiting');+ expect(shutdown.afterDrain?.error_text).toBe('worker_shutdown');+ expect(shutdown.drainForced).toBe(true);+}, 90_000);