Skip to content

Commit 68e7942

Browse files
authored
fix(node-host): drain all workers before shutdown failures (#129699)
1 parent 49f4240 commit 68e7942

2 files changed

Lines changed: 75 additions & 7 deletions

File tree

‎src/node-host/node-worker-supervisor.container.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
55
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
66
import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
77
import type { WorkerConnectionEndpoint } from "../worker/worker-connection-endpoint.js";
8+
import { NodeWorkerContainerLifecycle } from "./node-worker-container-lifecycle.js";
89
import { NodeWorkerLaunchStore } from "./node-worker-launch-store.js";
910
import { requireNodeWorkerProcessIdentity } from "./node-worker-process-identity.js";
1011
import { createNodeWorkerSupervisor } from "./node-worker-supervisor.js";
@@ -840,6 +841,73 @@ describe("node worker supervisor container isolation", () => {
840841
}
841842
});
842843

844+
it("waits for healthy container shutdown before reporting a sibling removal failure", async () => {
845+
const fixture = containerFixture({ capacity: 2 });
846+
const first = testWorkerLaunchInput(fixture.workspaceDir, "container-close-failed", "wait");
847+
const sibling = testWorkerLaunchInput(fixture.workspaceDir, "container-close-sibling", "wait");
848+
const removalMarker = path.join(fixture.engineRoot, "hold-removal");
849+
const store = new NodeWorkerLaunchStore({ env: fixture.env });
850+
const removalFailure = new Error("injected first container removal failure");
851+
const originalRemove = Reflect.get(
852+
NodeWorkerContainerLifecycle.prototype,
853+
"remove",
854+
) as NodeWorkerContainerLifecycle["remove"];
855+
const remove = vi
856+
.spyOn(NodeWorkerContainerLifecycle.prototype, "remove")
857+
.mockImplementation(async function (this: NodeWorkerContainerLifecycle, container, owner) {
858+
if (owner.launchId === first.launchId) {
859+
throw removalFailure;
860+
}
861+
await originalRemove.call(this, container, owner);
862+
});
863+
864+
try {
865+
const failedWorker = await fixture.supervisor.launch(first, endpoint);
866+
const siblingWorker = await fixture.supervisor.launch(sibling, endpoint);
867+
await vi.waitFor(
868+
() => expect(fixture.events().filter((event) => event.argv[0] === "start")).toHaveLength(2),
869+
{ timeout: 5_000 },
870+
);
871+
fs.writeFileSync(removalMarker, "hold");
872+
873+
const closing = fixture.supervisor.close();
874+
const settled = vi.fn();
875+
void closing.then(settled, settled);
876+
await vi.waitFor(
877+
() =>
878+
expect(
879+
fixture
880+
.events()
881+
.some(
882+
(event) =>
883+
event.argv[0] === "rm" &&
884+
event.argv.at(-1) === siblingWorker.container!.containerId,
885+
),
886+
).toBe(true),
887+
{ timeout: 5_000 },
888+
);
889+
890+
expect(settled).not.toHaveBeenCalled();
891+
expect(fixture.exists(siblingWorker.container!.containerId)).toBe(true);
892+
893+
fs.unlinkSync(removalMarker);
894+
await expect(closing).rejects.toBe(removalFailure);
895+
expect(fixture.exists(siblingWorker.container!.containerId)).toBe(false);
896+
expect(store.get(sibling.launchId)).toMatchObject({ state: "interrupted" });
897+
expect(fixture.exists(failedWorker.container!.containerId)).toBe(true);
898+
expect(store.get(first.launchId)).toMatchObject({
899+
state: "running",
900+
container: failedWorker.container,
901+
});
902+
} finally {
903+
if (fs.existsSync(removalMarker)) {
904+
fs.unlinkSync(removalMarker);
905+
}
906+
remove.mockRestore();
907+
await fixture.supervisor.close();
908+
}
909+
});
910+
843911
it("never executes a container worker when its durable identity cannot be recorded", async () => {
844912
const fixture = containerFixture();
845913
const input = testWorkerLaunchInput(fixture.workspaceDir, "container-journal-failure", "wait");

‎src/node-host/node-worker-supervisor.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,12 @@ class NodeWorkerSupervisor {
412412
}
413413
}
414414
await Promise.allSettled(this.starting.values());
415-
await Promise.all(
415+
const stopped = await Promise.allSettled(
416416
[...this.active.values()]
417417
.filter((active): active is NodeWorkerRunningChild => active.state === "running")
418-
.map(async (active) => await this.stopChild(active, "interrupted")),
418+
.map((active) => this.stopChild(active, "interrupted")),
419419
);
420+
errors.push(...stopped.flatMap((r) => (r.status === "rejected" ? [r.reason] : [])));
420421
for (const active of this.active.values()) {
421422
if (active.state !== "observed") {
422423
continue;
@@ -427,11 +428,10 @@ class NodeWorkerSupervisor {
427428
errors.push(error);
428429
}
429430
}
430-
if (errors.length === 1) {
431-
throw errors[0];
432-
}
433-
if (errors.length > 1) {
434-
throw new AggregateError(errors, "node worker terminal reconciliation failed");
431+
if (errors.length > 0) {
432+
throw errors.length === 1
433+
? errors[0]
434+
: new AggregateError(errors, "node worker terminal reconciliation failed");
435435
}
436436
})();
437437
const closePromise = operation.finally(() => {

0 commit comments

Comments
 (0)