Skip to content

Commit 4984713

Browse files
authored
fix(gateway): refresh health after runtime inspection failure (#127174)
1 parent 1dbcbb2 commit 4984713

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

‎src/cli/gateway-cli/health-route.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,38 @@ describe("runGatewayHealthJsonRoute", () => {
126126
expect(runtime.exit).toHaveBeenCalledWith(1);
127127
});
128128

129+
it("preserves structured Gateway health request errors", async () => {
130+
const runtime = createRuntime();
131+
const error = new Error("health snapshot unavailable");
132+
const callGateway = vi.fn(async () => {
133+
throw error;
134+
});
135+
const payload = {
136+
ok: false,
137+
error: {
138+
type: "gateway_request_error",
139+
code: "UNAVAILABLE",
140+
message: "health snapshot unavailable",
141+
},
142+
};
143+
const formatGatewayClientRequestErrorJson = vi.fn(() => payload);
144+
const formatGatewayTransportErrorJson = vi.fn();
145+
146+
await runGatewayHealthJsonRoute({ rpc: { json: true, timeout: "10000" } }, runtime as never, {
147+
callGateway,
148+
readNonObservingHealthConfig: async () => ({}),
149+
emitReachableGatewayAuthDiagnostic: vi.fn(async () => false) as never,
150+
formatGatewayAuthErrorJson: vi.fn(() => null) as never,
151+
formatGatewayClientRequestErrorJson: formatGatewayClientRequestErrorJson as never,
152+
formatGatewayTransportErrorJson: formatGatewayTransportErrorJson as never,
153+
});
154+
155+
expect(formatGatewayClientRequestErrorJson).toHaveBeenCalledWith(error);
156+
expect(formatGatewayTransportErrorJson).not.toHaveBeenCalled();
157+
expect(runtime.writeJson).toHaveBeenCalledWith(payload, 2);
158+
expect(runtime.exit).toHaveBeenCalledWith(1);
159+
});
160+
129161
it("preserves structured auth errors when reachability is unknown", async () => {
130162
const runtime = createRuntime();
131163
const error = new Error("gateway health requires credentials");

‎src/gateway/server-methods/health.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export const healthHandlers: GatewayRequestHandlers = {
140140
context.getRuntimeSnapshot(),
141141
);
142142
} catch {
143-
cachedDiffersFromRuntime = false;
143+
cachedDiffersFromRuntime = true;
144144
}
145145
}
146146
if (

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4575,6 +4575,53 @@ describe("gateway healthHandlers.health cache freshness", () => {
45754575
});
45764576
});
45774577

4578+
it("rejects cached health when runtime inspection and refresh both fail", async () => {
4579+
const cached = createHealthSnapshot({});
4580+
const refreshHealthSnapshot = vi.fn().mockRejectedValue(new Error("collector failed"));
4581+
const { respond } = await requestHealthSnapshot({
4582+
cached,
4583+
refreshHealthSnapshot,
4584+
context: {
4585+
getRuntimeSnapshot: () => {
4586+
throw new Error("runtime inspection failed");
4587+
},
4588+
},
4589+
});
4590+
4591+
expect(refreshHealthSnapshot).toHaveBeenCalledWith({
4592+
probe: false,
4593+
includeSensitive: false,
4594+
});
4595+
expect(respond).toHaveBeenCalledWith(
4596+
false,
4597+
undefined,
4598+
expect.objectContaining({
4599+
code: "UNAVAILABLE",
4600+
message: "Error: collector failed",
4601+
}),
4602+
);
4603+
});
4604+
4605+
it("refreshes cached health when runtime inspection fails", async () => {
4606+
const cached = createHealthSnapshot({});
4607+
const fresh = createHealthSnapshot({ ts: cached.ts + 1 });
4608+
const { respond, refreshHealthSnapshot } = await requestHealthSnapshot({
4609+
cached,
4610+
fresh,
4611+
context: {
4612+
getRuntimeSnapshot: () => {
4613+
throw new Error("runtime inspection failed");
4614+
},
4615+
},
4616+
});
4617+
4618+
expect(refreshHealthSnapshot).toHaveBeenCalledWith({
4619+
probe: false,
4620+
includeSensitive: false,
4621+
});
4622+
expect(respond).toHaveBeenCalledWith(true, fresh, undefined);
4623+
});
4624+
45784625
it("refreshes cached health when runtime channel lifecycle has changed", async () => {
45794626
const cached = createSingleChannelHealthSnapshot({
45804627
channelId: "discord",

0 commit comments

Comments
 (0)