Skip to content

Commit 0661763

Browse files
authored
fix(gateway): avoid failed worker cleanup blocking startup (#128103)
* fix(gateway): defer failed worker cleanup after readiness * test(gateway): preserve periodic worker recovery ordering * fix(gateway): track post-ready session retirement * fix(gateway): defer failed teardown past readiness
1 parent 2aa4b69 commit 0661763

11 files changed

Lines changed: 529 additions & 43 deletions
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { installWorkerPlacementReconcileGuard } from "./server-worker-placement-reconcile-guard.js";
3+
4+
const localClaim = {
5+
owner: "local" as const,
6+
claimId: "claim-cleanup",
7+
runId: "run-cleanup",
8+
generation: 1,
9+
ownerEpoch: null,
10+
};
11+
12+
function createFailedPlacementGuard(params: {
13+
turnClaim: typeof localClaim | null;
14+
activeOwnerEpoch: number | null;
15+
destroyRequestedAtMs: number | null;
16+
}) {
17+
let guard:
18+
| ((environmentId: string, reconcileCore: () => Promise<void>) => Promise<void>)
19+
| undefined;
20+
const resumeProvisioning = vi.fn();
21+
installWorkerPlacementReconcileGuard({
22+
placements: {
23+
list: () => [
24+
{
25+
sessionId: "session-cleanup",
26+
state: "failed",
27+
environmentId: "worker-cleanup",
28+
turnClaim: params.turnClaim,
29+
activeOwnerEpoch: params.activeOwnerEpoch,
30+
},
31+
],
32+
} as never,
33+
environments: {
34+
get: (environmentId: string) => ({
35+
environmentId,
36+
state: "provisioning",
37+
destroyRequestedAtMs: params.destroyRequestedAtMs,
38+
}),
39+
installReconcileEnvironmentGuard: (installed: typeof guard) => {
40+
guard = installed;
41+
return async () => {};
42+
},
43+
} as never,
44+
dispatch: { resumeProvisioning } as never,
45+
isStopping: () => false,
46+
});
47+
if (!guard) {
48+
throw new Error("worker placement reconciliation guard was not installed");
49+
}
50+
return { guard, resumeProvisioning };
51+
}
52+
53+
describe("worker placement reconciliation teardown authority", () => {
54+
it("allows destruction only after its exact failed owner has released all authority", async () => {
55+
const { guard, resumeProvisioning } = createFailedPlacementGuard({
56+
turnClaim: null,
57+
activeOwnerEpoch: null,
58+
destroyRequestedAtMs: 1,
59+
});
60+
const reconcileCore = vi.fn(async () => {});
61+
62+
await guard("worker-cleanup", reconcileCore);
63+
64+
expect(reconcileCore).toHaveBeenCalledOnce();
65+
expect(resumeProvisioning).not.toHaveBeenCalled();
66+
});
67+
68+
it.each([
69+
{
70+
reason: "a retained local turn claim",
71+
turnClaim: localClaim,
72+
activeOwnerEpoch: null,
73+
destroyRequestedAtMs: 1,
74+
},
75+
{
76+
reason: "an active owner epoch",
77+
turnClaim: null,
78+
activeOwnerEpoch: 2,
79+
destroyRequestedAtMs: 1,
80+
},
81+
{
82+
reason: "no durable destruction request",
83+
turnClaim: null,
84+
activeOwnerEpoch: null,
85+
destroyRequestedAtMs: null,
86+
},
87+
])("keeps failed-placement cleanup fenced with $reason", async ({ reason: _, ...params }) => {
88+
const { guard, resumeProvisioning } = createFailedPlacementGuard(params);
89+
const reconcileCore = vi.fn(async () => {});
90+
91+
await expect(guard("worker-cleanup", reconcileCore)).rejects.toThrow(
92+
"provisioning owner is failed",
93+
);
94+
95+
expect(reconcileCore).not.toHaveBeenCalled();
96+
expect(resumeProvisioning).not.toHaveBeenCalled();
97+
});
98+
});

‎src/gateway/server-worker-placement-reconcile-guard.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export function installWorkerPlacementReconcileGuard(params: {
2929
owner &&
3030
(environment?.state === "requested" ||
3131
environment?.state === "provisioning" ||
32-
environment?.state === "bootstrapping")
32+
environment?.state === "bootstrapping") &&
33+
(owner.state !== "failed" ||
34+
owner.turnClaim !== null ||
35+
owner.activeOwnerEpoch !== null ||
36+
environment.destroyRequestedAtMs === null)
3337
) {
3438
throw new Error(`Worker environment ${environmentId} provisioning owner is ${owner.state}`);
3539
}

‎src/gateway/server-worker-placement-recovery-events.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ describe("worker placement recovery session events", () => {
9797
throw new Error("worker placement runtime did not start");
9898
}
9999

100+
await vi.dynamicImportSettled();
100101
await vi.advanceTimersByTimeAsync(60_000);
101102
expect(context.broadcastToConnIds).not.toHaveBeenCalled();
102103
expect(readSessionsMutationVersion(context)).toBe(initialMutationVersion);

0 commit comments

Comments
 (0)