Skip to content

Commit 7072ba5

Browse files
fix(gateway): shutdown reports unresolved SecretRef warnings when channel credentials use exec/file refs (#112717)
* fix(gateway): clear secrets runtime snapshot only after channel teardown Shutdown cleared the secrets runtime snapshot in the close prelude, which also drops the pinned runtime config. The channels close step then lazily re-pins the raw config, so stopChannel's resolveAccount hit unresolved SecretRefs and every shutdown of a gateway with an exec/file SecretRef credential logged an unresolved-SecretRef WARN and completed with warnings. The failed resolveAccount also skipped the per-account stop bookkeeping (graceful wait, task/abort cleanup, stopped-runtime state) and the plugin stopAccount hook. Move the scrub into the close handler's finally block so it still always runs, but only after every step that can read credentials. Fixes #112681 * chore: drop release-owned changelog entry --------- Co-authored-by: FullerStackDev <[email protected]>
1 parent 5d4ff17 commit 7072ba5

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎src/gateway/server-close.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,45 @@ describe("createGatewayCloseHandler", () => {
281281
expect(stopChannel).toHaveBeenCalledWith("discord");
282282
});
283283

284+
it("clears the secrets runtime snapshot only after channels stop (#112681)", async () => {
285+
const events: string[] = [];
286+
const stopChannel = vi.fn(async (channelId: string) => {
287+
events.push(`channel:${channelId}`);
288+
});
289+
const clearSecretsRuntimeSnapshot = vi.fn(() => {
290+
events.push("clear-secrets");
291+
});
292+
const close = createGatewayCloseHandler(
293+
createGatewayCloseTestDeps({
294+
channelIds: ["telegram"],
295+
stopChannel,
296+
clearSecretsRuntimeSnapshot,
297+
}),
298+
);
299+
300+
await close({ reason: "test" });
301+
302+
expect(events).toEqual(["channel:telegram", "clear-secrets"]);
303+
});
304+
305+
it("clears the secrets runtime snapshot even when a channel stop fails", async () => {
306+
const clearSecretsRuntimeSnapshot = vi.fn();
307+
const close = createGatewayCloseHandler(
308+
createGatewayCloseTestDeps({
309+
channelIds: ["telegram"],
310+
stopChannel: vi.fn(async () => {
311+
throw new Error("stop failed");
312+
}),
313+
clearSecretsRuntimeSnapshot,
314+
}),
315+
);
316+
317+
const result = await close({ reason: "test" });
318+
319+
expect(clearSecretsRuntimeSnapshot).toHaveBeenCalledTimes(1);
320+
expect(result.warnings).toContain("channel/telegram");
321+
});
322+
284323
it("awaits post-ready sidecars before plugin services and channels", async () => {
285324
const events: string[] = [];
286325
let releaseSidecar!: () => void;

‎src/gateway/server-close.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,6 @@ export async function runGatewayClosePrelude(params: {
619619
disposeBrowserAuthRateLimiter: () => void;
620620
stopChannelHealthMonitor?: () => Promise<void>;
621621
stopReadinessEventLoopHealth?: () => void;
622-
clearSecretsRuntimeSnapshot?: () => void;
623622
closeMcpServer?: () => Promise<void>;
624623
}): Promise<void> {
625624
params.stopDiagnostics?.();
@@ -629,7 +628,6 @@ export async function runGatewayClosePrelude(params: {
629628
params.disposeBrowserAuthRateLimiter();
630629
await params.stopChannelHealthMonitor?.();
631630
params.stopReadinessEventLoopHealth?.();
632-
params.clearSecretsRuntimeSnapshot?.();
633631
await params.closeMcpServer?.().catch(() => {});
634632
}
635633

@@ -674,6 +672,7 @@ export function createGatewayCloseHandler(
674672
bonjourStop: (() => Promise<void>) | null;
675673
tailscaleCleanup: (() => Promise<void>) | null;
676674
releasePluginRouteRegistry?: (() => void) | null;
675+
clearSecretsRuntimeSnapshot?: (() => void) | null;
677676
channelIds?: readonly ChannelId[];
678677
stopChannel: (name: ChannelId, accountId?: string) => Promise<void>;
679678
pluginServices: PluginServicesHandle | null;
@@ -1043,6 +1042,13 @@ export function createGatewayCloseHandler(
10431042
} catch {
10441043
/* ignore */
10451044
}
1045+
// Channel and plugin teardown still resolve account credentials. Keep the
1046+
// active snapshot until every teardown owner is done, then always scrub it.
1047+
try {
1048+
params.clearSecretsRuntimeSnapshot?.();
1049+
} catch {
1050+
/* ignore */
1051+
}
10461052
}
10471053

10481054
const durationMs = Date.now() - start;

‎src/gateway/server-lifecycle.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ export async function prepareGatewayLifecycle(params: {
370370
await monitor?.waitForIdle();
371371
},
372372
stopReadinessEventLoopHealth: readinessEventLoopHealth.stop,
373-
clearSecretsRuntimeSnapshot,
374373
closeMcpServer: closeMcpLoopbackServerOnDemand,
375374
});
376375
};
@@ -407,6 +406,7 @@ export async function prepareGatewayLifecycle(params: {
407406
bonjourStop: runtimeState.bonjourStop,
408407
tailscaleCleanup: runtimeState.tailscaleCleanup,
409408
releasePluginRouteRegistry,
409+
clearSecretsRuntimeSnapshot,
410410
channelIds,
411411
stopChannel,
412412
pluginServices: runtimeState.pluginServices,

0 commit comments

Comments
 (0)