Skip to content

Commit 1b84119

Browse files
authored
fix(gateway): stop post-ready sidecar timers on close (#117242)
* fix(gateway): stop post-ready sidecar timers on close * fix(gateway): return promise from generation timer admission callback
1 parent 6fb334f commit 1b84119

2 files changed

Lines changed: 202 additions & 96 deletions

File tree

‎src/gateway/server-startup-post-attach.test.ts‎

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ describe("startGatewayPostAttachRuntime", () => {
537537
await startGatewayPostAttachRuntime({
538538
...createPostAttachParams(),
539539
log,
540-
agentRuntimePluginPrewarm: { enabled: false },
541540
onPostReadySidecars: () => {
542541
events.push("post-ready-registered");
543542
},
@@ -622,7 +621,7 @@ describe("startGatewayPostAttachRuntime", () => {
622621
});
623622
hoisted.scheduleRestartSentinelWake.mockReturnValueOnce(wake);
624623

625-
testing.scheduleRestartSentinelWakeAfterReady({
624+
const sidecar = testing.scheduleRestartSentinelWakeAfterReady({
626625
deps: {} as never,
627626
log: { warn: vi.fn() },
628627
});
@@ -635,6 +634,20 @@ describe("startGatewayPostAttachRuntime", () => {
635634
await waitForGatewayTestState(() => {
636635
expect(getActiveGatewayRootWorkCount()).toBe(0);
637636
});
637+
await sidecar.stop();
638+
});
639+
640+
it("cancels delayed restart sentinel recovery when the gateway closes", async () => {
641+
vi.useFakeTimers();
642+
const sidecar = testing.scheduleRestartSentinelWakeAfterReady({
643+
deps: {} as never,
644+
log: { warn: vi.fn() },
645+
});
646+
647+
await sidecar.stop();
648+
await vi.advanceTimersByTimeAsync(750);
649+
650+
expect(hoisted.scheduleRestartSentinelWake).not.toHaveBeenCalled();
638651
});
639652

640653
it("starts sidecars while startup logging is still pending", async () => {
@@ -1172,7 +1185,7 @@ describe("startGatewayPostAttachRuntime", () => {
11721185
await vi.advanceTimersToNextTimerAsync();
11731186
await vi.advanceTimersToNextTimerAsync();
11741187
expect(postReadyRequestTurn).toHaveBeenCalledTimes(1);
1175-
expect(onPostReadySidecars.mock.calls[0]?.[0]).toHaveLength(0);
1188+
expect(onPostReadySidecars.mock.calls[0]?.[0]).toHaveLength(2);
11761189
expect(onGatewayLifetimeSidecars.mock.calls[0]?.[0]).toHaveLength(4);
11771190
await vi.dynamicImportSettled();
11781191
await waitForGatewayTestState(() => {
@@ -1236,10 +1249,8 @@ describe("startGatewayPostAttachRuntime", () => {
12361249
gatewayPluginConfigAtStart: startupConfig,
12371250
}),
12381251
waitForPostReadyWork: () => gatewayReady,
1239-
providerAuthPrewarm: { enabled: false },
1240-
agentRuntimePluginPrewarm: {
1241-
enabled: true,
1242-
delayMs: 0,
1252+
providerAuthPrewarm: {
1253+
enabled: false,
12431254
getConfig: () => currentConfig,
12441255
},
12451256
});
@@ -1344,7 +1355,7 @@ describe("startGatewayPostAttachRuntime", () => {
13441355
const lifetimeSidecars = onGatewayLifetimeSidecars.mock.calls[0]?.[0] as
13451356
| { stop: () => void }[]
13461357
| undefined;
1347-
expect(gmailSidecars).toHaveLength(1);
1358+
expect(gmailSidecars).toHaveLength(3);
13481359
expect(lifetimeSidecars).toHaveLength(4);
13491360

13501361
for (const sidecar of gmailSidecars ?? []) {
@@ -1406,7 +1417,7 @@ describe("startGatewayPostAttachRuntime", () => {
14061417
const lifetimeSidecars = onGatewayLifetimeSidecars.mock.calls[0]?.[0] as
14071418
| Array<{ stop: () => Promise<void> | void }>
14081419
| undefined;
1409-
expect(gmailSidecars).toHaveLength(1);
1420+
expect(gmailSidecars).toHaveLength(3);
14101421
expect(lifetimeSidecars).toHaveLength(4);
14111422

14121423
await waitForGatewayTestState(() => {
@@ -2003,7 +2014,7 @@ describe("startGatewayPostAttachRuntime", () => {
20032014
name: "sidecars.ready",
20042015
metrics: [
20052016
["loadedPluginCount", 2],
2006-
["postReadySidecarCount", 3],
2017+
["postReadySidecarCount", 5],
20072018
],
20082019
});
20092020
});
@@ -2064,7 +2075,7 @@ describe("startGatewayPostAttachRuntime", () => {
20642075
},
20652076
});
20662077

2067-
expect(result.postReadySidecars).toHaveLength(1);
2078+
expect(result.postReadySidecars).toHaveLength(3);
20682079
expect(hoisted.startGmailWatcherWithLogs).not.toHaveBeenCalled();
20692080
onPostReadySidecars(result.postReadySidecars);
20702081
expect(onPostReadySidecars).toHaveBeenCalledWith(result.postReadySidecars);
@@ -2078,7 +2089,9 @@ describe("startGatewayPostAttachRuntime", () => {
20782089
if (!resolveWatcher) {
20792090
throw new Error("Expected gmail watcher resolver to be initialized");
20802091
}
2081-
await result.postReadySidecars[0]?.stop();
2092+
for (const sidecar of result.postReadySidecars) {
2093+
await sidecar.stop();
2094+
}
20822095
expect(watcherSignal?.aborted).toBe(true);
20832096
resolveWatcher();
20842097
});
@@ -2107,7 +2120,7 @@ describe("startGatewayPostAttachRuntime", () => {
21072120
},
21082121
});
21092122

2110-
expect(result.postReadySidecars).toHaveLength(1);
2123+
expect(result.postReadySidecars).toHaveLength(3);
21112124
await waitForGatewayTestState(() => {
21122125
expect(log.warn).toHaveBeenCalledWith(
21132126
"sidecars.gmail-watch failed after gateway ready: Error: boom",
@@ -2136,8 +2149,10 @@ describe("startGatewayPostAttachRuntime", () => {
21362149
},
21372150
});
21382151

2139-
expect(result.postReadySidecars).toHaveLength(1);
2140-
await result.postReadySidecars[0]?.stop();
2152+
expect(result.postReadySidecars).toHaveLength(3);
2153+
for (const sidecar of result.postReadySidecars) {
2154+
await sidecar.stop();
2155+
}
21412156
await new Promise<void>((resolve) => {
21422157
setImmediate(resolve);
21432158
});
@@ -2183,7 +2198,9 @@ describe("startGatewayPostAttachRuntime", () => {
21832198
await waitForGatewayTestState(() => {
21842199
expect(releaseImport).toBeDefined();
21852200
});
2186-
await result.postReadySidecars[0]?.stop();
2201+
for (const sidecar of result.postReadySidecars) {
2202+
await sidecar.stop();
2203+
}
21872204
releaseImport?.();
21882205
await new Promise<void>((resolve) => {
21892206
setImmediate(resolve);
@@ -2263,7 +2280,7 @@ describe("startGatewayPostAttachRuntime", () => {
22632280
},
22642281
});
22652282

2266-
expect(result.postReadySidecars).toHaveLength(1);
2283+
expect(result.postReadySidecars).toHaveLength(3);
22672284
expect(hoisted.loadModelCatalog).not.toHaveBeenCalled();
22682285

22692286
await waitForGatewayTestState(() => {
@@ -2525,6 +2542,49 @@ describe("startGatewayPostAttachRuntime", () => {
25252542
}
25262543
});
25272544

2545+
it("cancels registered gateway startup hooks when close starts", async () => {
2546+
vi.useFakeTimers();
2547+
hoisted.hasInternalHookListeners.mockReturnValue(true);
2548+
const trace = createStartupTraceRecorder();
2549+
let releasePostReadyWork!: () => void;
2550+
const postReadyWork = new Promise<void>((resolve) => {
2551+
releasePostReadyWork = resolve;
2552+
});
2553+
2554+
const result = await startGatewaySidecars({
2555+
cfg: {} as never,
2556+
pluginRegistry: createPostAttachParams().pluginRegistry,
2557+
defaultWorkspaceDir: "/tmp/openclaw-workspace",
2558+
deps: {} as never,
2559+
startChannels: vi.fn(async () => {}),
2560+
log: { warn: vi.fn() },
2561+
logHooks: {
2562+
info: vi.fn(),
2563+
warn: vi.fn(),
2564+
error: vi.fn(),
2565+
},
2566+
logChannels: {
2567+
info: vi.fn(),
2568+
error: vi.fn(),
2569+
},
2570+
startupTrace: trace.startupTrace,
2571+
waitForPostReadyWork: () => postReadyWork,
2572+
});
2573+
2574+
expect(result.postReadySidecars).toHaveLength(3);
2575+
testing.stopPostReadySidecarsAfterCloseStarted({
2576+
postReadySidecars: result.postReadySidecars,
2577+
closeStarted: true,
2578+
});
2579+
releasePostReadyWork();
2580+
await vi.advanceTimersByTimeAsync(1_000);
2581+
2582+
expect(hoisted.createInternalHookEvent).not.toHaveBeenCalled();
2583+
expect(hoisted.triggerInternalHook).not.toHaveBeenCalled();
2584+
expect(trace.measures).not.toContain("sidecars.session-locks");
2585+
expect(trace.measures).not.toContain("sidecars.restart-sentinel");
2586+
});
2587+
25282588
it("waits for a healthy ACP runtime backend before startup identity reconcile", async () => {
25292589
const trace = createStartupTraceRecorder();
25302590
let healthy = false;

0 commit comments

Comments
 (0)