From 6a770ce096417c68c57caceba73c7716e9de65c1 Mon Sep 17 00:00:00 2001 From: Devin Foley Date: Thu, 8 Oct 2026 11:51:06 -0700 Subject: [PATCH] Resume plugin crash recovery after idle holds Keep a crashed worker's retry scheduled while an idle hold is active. Accepted startup work remains tracked instead of failing permanently. A recovered worker still cannot prove lost completion receipts. Verified all three real-process idle tests, including crash recovery through a hold and service restoration after release. Co-Authored-By: Paperclip --- .../__tests__/fixtures/plugin-worker-idle.cjs | 1 + .../src/__tests__/plugin-idle-manager.test.ts | 17 +++++++++++++++++ server/src/services/plugin-worker-manager.ts | 11 ++++++++--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/server/src/__tests__/fixtures/plugin-worker-idle.cjs b/server/src/__tests__/fixtures/plugin-worker-idle.cjs index 40364b7406a..c17c40610f6 100644 --- a/server/src/__tests__/fixtures/plugin-worker-idle.cjs +++ b/server/src/__tests__/fixtures/plugin-worker-idle.cjs @@ -14,6 +14,7 @@ void Promise.all([ return { status: "ok" }; }, async onEnvironmentProbe(params) { + if (params.config.crash) process.exit(1); await new Promise((resolve) => setTimeout(resolve, params.config.delayMs)); return { ok: true, summary: "fixture complete" }; }, diff --git a/server/src/__tests__/plugin-idle-manager.test.ts b/server/src/__tests__/plugin-idle-manager.test.ts index 3c2420df157..e8bbb7ee019 100644 --- a/server/src/__tests__/plugin-idle-manager.test.ts +++ b/server/src/__tests__/plugin-idle-manager.test.ts @@ -1,5 +1,6 @@ import { fileURLToPath } from "node:url"; import { createRequire } from "node:module"; +import { setTimeout as delay } from "node:timers/promises"; import { afterEach, describe, expect, it } from "vitest"; import { createPluginWorkerHandle } from "../services/plugin-worker-manager.js"; import { startTaskDrain, stopTaskDrain } from "../services/task-admission.js"; @@ -53,4 +54,20 @@ describe("plugin manager idle receipts", () => { await expect.poll(() => handle.prepareIdleSleep!(lease)).toBe("none"); } finally { stopTaskDrain(); await handle.stop(); } }); + + it("resumes crash recovery after an idle hold without discarding the restart", async () => { + const handle = worker(); + await handle.start(); + handle.on("crash", () => { hold(); }); + try { + await expect(handle.call("environmentProbe", { driverKey: "fixture", companyId: "fixture", environmentId: "fixture", config: { crash: true } })).rejects.toThrow(); + await delay(1_600); + expect(handle.status).toBe("backoff"); + expect(handle.diagnostics().nextRestartAt).not.toBeNull(); + stopTaskDrain(); + await expect.poll(() => handle.status, { timeout: 5_000 }).toBe("running"); + // Recovery restores service, but lost completion receipts remain unsafe. + expect(await handle.prepareIdleSleep!(hold())).toBe("unknown"); + } finally { stopTaskDrain(); await handle.stop(); } + }); }); diff --git a/server/src/services/plugin-worker-manager.ts b/server/src/services/plugin-worker-manager.ts index 7ac5c25fc9d..d4ac6e919af 100644 --- a/server/src/services/plugin-worker-manager.ts +++ b/server/src/services/plugin-worker-manager.ts @@ -1049,10 +1049,10 @@ export function createPluginWorkerHandle( // ----------------------------------------------------------------------- function setStatus(newStatus: WorkerStatus): void { - const done = beginIdleTrackedWork(); - done(); const prev = status; if (prev === newStatus) return; + const done = beginIdleTrackedWork(); + done(); status = newStatus; log.debug({ from: prev, to: newStatus }, "worker status change"); emitter.emit("status", { pluginId, status: newStatus, previousStatus: prev }); @@ -3121,6 +3121,12 @@ export function createPluginWorkerHandle( backoffTimer = setTimeout(async () => { backoffTimer = null; nextRestartAt = null; + // A temporary idle hold must not consume the crash-recovery attempt. + // Keep the bounded backoff until admission reopens. + if (readTaskDrain(new Date())?.ownerId) { + scheduleRestart(); + return; + } try { await startInternal(); } catch (err) { @@ -3145,7 +3151,6 @@ export function createPluginWorkerHandle( // ----------------------------------------------------------------------- async function startInternal(): Promise { - if (readTaskDrain(new Date())?.ownerId) throw new Error("Plugin start is held for idle sleep"); if (status === "running" || status === "starting") { throw new Error(`Worker for plugin "${pluginId}" is already ${status}`); }