From 334c7bd8825955f4baa3887e4241d9a79f31209f Mon Sep 17 00:00:00 2001 From: Muthiah Muralidharan Date: Wed, 18 Nov 2026 21:21:31 +0530 Subject: [PATCH 1/2] fix(listen): report reconnects as reconnected and recover expired sessions A listen session that dropped and then reconnected was recorded as listen_connect_error, the same as an attempt that never connected. A laptop that sleeps with listen running reconnects on every wake, so one machine could fill the error dashboard with successful reconnects. - The first subscribe of a run records listen_phase "established"; a later subscribe after a drop records "reconnected" (status ok). listen_connect_error is recorded only when an attempt fails to subscribe, once per outage. - A heartbeat refused with CONTROL_FAILED::404 means the session expired on Chargebee's side (it lives 90s after the last accepted heartbeat and a heartbeat cannot revive it). listen now closes the socket and reconnects, which registers the session again. Before, a pause between 90s and the 5-minute idle threshold left listen connected but receiving nothing, warning on every heartbeat. - A heartbeat refused as unauthorized ends the run with the same message as a refused connect. - Other refused heartbeats keep the socket as before, but warn once ("Session keepalive failed; retrying") and say when heartbeats are accepted again, instead of warning every 30 seconds. Co-Authored-By: Claude Opus 5.5 --- TELEMETRY.md | 2 +- src/lib/telemetry/index.ts | 2 +- src/lib/telemetry/metadata.ts | 2 +- src/lib/tunnel/appsync-errors.ts | 10 ++ src/lib/tunnel/appsync.ts | 69 +++++++- .../unit/lib/tunnel/appsync-errors.test.ts | 15 ++ src/tests/unit/lib/tunnel/appsync.test.ts | 156 +++++++++++++++++- 7 files changed, 235 insertions(+), 21 deletions(-) diff --git a/TELEMETRY.md b/TELEMETRY.md index e1b5a0b..e14e740 100644 --- a/TELEMETRY.md +++ b/TELEMETRY.md @@ -84,7 +84,7 @@ A telemetry request has the following structure: | `pcv` | When available | Product Catalog version (`v1` or `v2`) of the configured site. | | `code_lang` | Code generation | Generated code language, such as `go` or `js`. | | `agent` | When detected | Supported agent runtime: `claude-code`, `cursor`, `codex` (sandboxed runs) or `gemini-cli`, detected from environment variables those tools set. | -| `listen_phase` | `chargebee listen` | Tunnel lifecycle state: `established`, `closed`, or `error`. | +| `listen_phase` | `chargebee listen` | Tunnel lifecycle state: `established` (first connection), `reconnected` (connected again after a drop), `closed`, or `error` (a connection attempt failed). | | `first_run` | First recorded event only | Date (`YYYY-MM-DD`) the first-run notice was shown. | ## What is Never Collected diff --git a/src/lib/telemetry/index.ts b/src/lib/telemetry/index.ts index 8659b39..61f222a 100644 --- a/src/lib/telemetry/index.ts +++ b/src/lib/telemetry/index.ts @@ -28,7 +28,7 @@ import { detectInstallMethod } from "../update/index.js"; import { knownCommandPath } from "./command-path.js"; import type { SpoolRecord } from "./types.js"; -export type ListenPhase = "established" | "closed" | "error"; +export type ListenPhase = "established" | "reconnected" | "closed" | "error"; /** Process start — duration is measured from here so it covers parse + action. */ const PROCESS_START = Date.now(); diff --git a/src/lib/telemetry/metadata.ts b/src/lib/telemetry/metadata.ts index f21121d..87d29ac 100644 --- a/src/lib/telemetry/metadata.ts +++ b/src/lib/telemetry/metadata.ts @@ -14,7 +14,7 @@ export interface MetadataInput { errorType?: string; productCatalogVersion?: string; generatedResource?: string; - listenPhase?: "established" | "closed" | "error"; + listenPhase?: "established" | "reconnected" | "closed" | "error"; /** How this copy was installed (npm, github, pnpm, ...), from the update module. */ installMethod?: string; /** Date (YYYY-MM-DD) the first-run notice was shown; set only on an install's first event. */ diff --git a/src/lib/tunnel/appsync-errors.ts b/src/lib/tunnel/appsync-errors.ts index 010bd66..d69a6cf 100644 --- a/src/lib/tunnel/appsync-errors.ts +++ b/src/lib/tunnel/appsync-errors.ts @@ -137,6 +137,16 @@ export function classifyAppSyncFailure(msg: AppSyncErrorMessage): AppSyncFailure }; } +/** + * True when a heartbeat was refused because Chargebee no longer holds the + * session (`CONTROL_FAILED::404`). Sessions expire 90 s after the last + * accepted heartbeat, and a heartbeat cannot revive one; only a new subscribe + * registers it again. + */ +export function isSessionExpired(failure: AppSyncFailure): boolean { + return failure.code === AppSyncErrorCode.CONTROL_FAILED && httpStatus(failure.detail) === 404; +} + /** * User-facing copy. No vendor or pipeline names (AppSync, Lambda, authorizer). */ diff --git a/src/lib/tunnel/appsync.ts b/src/lib/tunnel/appsync.ts index 819e4d1..9aa08dc 100644 --- a/src/lib/tunnel/appsync.ts +++ b/src/lib/tunnel/appsync.ts @@ -3,7 +3,12 @@ import { randomUUID } from "node:crypto"; import type { WebhookMsg } from "../protocol/types.js"; import { emitListenPhase } from "../telemetry/index.js"; -import { AppSyncErrorCode, classifyAppSyncFailure, listenUserMessage } from "./appsync-errors.js"; +import { + AppSyncErrorCode, + classifyAppSyncFailure, + isSessionExpired, + listenUserMessage, +} from "./appsync-errors.js"; import { forward } from "./forwarder.js"; import * as display from "./display.js"; @@ -347,13 +352,19 @@ export async function runAppSync(opts: AppSyncOptions): Promise { +/** Disconnect reason after a heartbeat finds the session gone on Chargebee's side. */ +const SESSION_EXPIRED_REASON = "The listen session expired"; + +function connectAppSync( + opts: AppSyncOptions, + forwardQueue: ForwardQueue, + subscribedPhase: "established" | "reconnected", +): Promise { const { httpDomain, realtimeDomain, @@ -498,6 +520,32 @@ function connectAppSync(opts: AppSyncOptions, forwardQueue: ForwardQueue): Promi closeSocket(); }, connectTimeoutMs); + /** True from the first refused heartbeat until one is accepted again. */ + let keepaliveFailing = false; + + /** + * A refused heartbeat. The session gone (404) → reconnect, which registers + * it again. The key or site denied (401/403) → stop, as on connect. + * Anything else → keep the socket; the next heartbeat retries. Warn once + * per run of failures, not on every heartbeat. + */ + const onKeepaliveFailure = (msg: ServerMessage) => { + const failure = classifyAppSyncFailure(msg); + if (failure.code === AppSyncErrorCode.APPSYNC_UNAUTHORIZED) { + finish({ kind: "permanent", reason: failure.reason, code: failure.code }); + closeSocket(); + return; + } + if (isSessionExpired(failure)) { + finish({ kind: "transient", reason: SESSION_EXPIRED_REASON, subscribedMs: subscribedMs() }); + closeSocket(); + return; + } + if (keepaliveFailing) return; + keepaliveFailing = true; + display.warn("Session keepalive failed; retrying"); + }; + const armIdleTimer = () => { if (finished || idleThresholdMs <= 0) return; if (idleTimer !== undefined) clearTimeout(idleTimer); @@ -546,7 +594,7 @@ function connectAppSync(opts: AppSyncOptions, forwardQueue: ForwardQueue): Promi connectDeadline = undefined; subscribedAt = Date.now(); display.ready(forwardTo); - emitListenPhase("established"); + emitListenPhase(subscribedPhase); publishControl("heartbeat"); heartbeat = setInterval(() => publishControl("heartbeat"), heartbeatMs); armIdleTimer(); @@ -569,11 +617,14 @@ function connectAppSync(opts: AppSyncOptions, forwardQueue: ForwardQueue): Promi break; } case "publish_error": - display.warn("Session keepalive failed"); + onKeepaliveFailure(msg); break; case "publish_success": if (Array.isArray(msg.failed) && msg.failed.length > 0) { - display.warn("Session keepalive failed"); + onKeepaliveFailure({ errors: msg.failed }); + } else if (keepaliveFailing) { + keepaliveFailing = false; + display.warn("Session keepalive restored"); } break; case "broadcast_error": diff --git a/src/tests/unit/lib/tunnel/appsync-errors.test.ts b/src/tests/unit/lib/tunnel/appsync-errors.test.ts index 19430d9..0a4d6e8 100644 --- a/src/tests/unit/lib/tunnel/appsync-errors.test.ts +++ b/src/tests/unit/lib/tunnel/appsync-errors.test.ts @@ -4,6 +4,7 @@ import { AppSyncErrorCode, classifyAppSyncFailure, handlerError, + isSessionExpired, listenUserMessage, parseHandlerError, } from "../../../../lib/tunnel/appsync-errors.js"; @@ -103,6 +104,20 @@ describe("classifyAppSyncFailure", () => { }); }); +describe("isSessionExpired", () => { + const classify = (message: string) => classifyAppSyncFailure({ errors: [{ message }] }); + + it("is true only for a CONTROL_FAILED heartbeat that ended in 404", () => { + expect(isSessionExpired(classify("CONTROL_FAILED:heartbeat:404"))).toBe(true); + expect(isSessionExpired(classify("CONTROL_FAILED:heartbeat:503"))).toBe(false); + expect(isSessionExpired(classify("CONTROL_FAILED:heartbeat"))).toBe(false); + expect(isSessionExpired(classify("ENABLE_FAILED:404"))).toBe(false); + expect(isSessionExpired(classifyAppSyncFailure({ errors: [{ errorType: "UnauthorizedException" }] }))).toBe( + false, + ); + }); +}); + describe("listenUserMessage", () => { it("does not name the pipeline or blame the API key for a 401", () => { const message = listenUserMessage(AppSyncErrorCode.APPSYNC_UNAUTHORIZED); diff --git a/src/tests/unit/lib/tunnel/appsync.test.ts b/src/tests/unit/lib/tunnel/appsync.test.ts index 9b8b62c..63a46f4 100644 --- a/src/tests/unit/lib/tunnel/appsync.test.ts +++ b/src/tests/unit/lib/tunnel/appsync.test.ts @@ -381,7 +381,7 @@ describe("runAppSync (fake WebSocket)", () => { ws.emit({ type: "publish_error", errors: [{ message: "throttled" }] }); ws.emit({ type: "data", event: { event_type: "subscription_created" } }); await waitUntil(() => logs.some((l) => l.includes("failed:"))); - expect(logs).toContain("> Session keepalive failed"); + expect(logs).toContain("> Session keepalive failed; retrying"); expect( logs.some((l) => l.includes("subscription_created \u2192 failed:") && @@ -436,7 +436,8 @@ describe("runAppSync (fake WebSocket)", () => { ws.emit({ type: "publish_success", failed: [{ index: 0 }] }); ws.emit({ type: "publish_success", failed: [] }); expect(logs).toContain("> A webhook event could not be delivered"); - expect(logs.filter((l) => l === "> Session keepalive failed")).toHaveLength(1); + expect(logs.filter((l) => l === "> Session keepalive failed; retrying")).toHaveLength(1); + expect(logs.filter((l) => l === "> Session keepalive restored")).toHaveLength(1); ws.emit({ type: "data", @@ -634,7 +635,7 @@ describe("runAppSync reliability (fake WebSocket)", () => { FakeWebSocket.instances = []; const { opts, ac } = baseOpts(); const done = runAppSync(opts); - // Never emit connection_ack � the deadline must fire on its own. + // Never emit connection_ack — the deadline must fire on its own. await waitUntil(() => FakeWebSocket.instances.length >= 2, 2000); expect( logs.some((l) => @@ -664,7 +665,7 @@ describe("runAppSync reliability (fake WebSocket)", () => { const { ws, ac, done } = await handshake(); expect(ws.readyState).toBe(1); - // No "ka" or any other frame arrives � the socket goes idle after subscribing. + // No "ka" or any other frame arrives — the socket goes idle after subscribing. await waitUntil(() => FakeWebSocket.instances.length >= 2, 2000); expect(ws.readyState).toBe(3); expect( @@ -814,7 +815,7 @@ describe("runAppSync reliability (fake WebSocket)", () => { } }); - test("emits one telemetry error phase per outage, not one per failed attempt", async () => { + test("reports failed attempts once per outage, and a recovered drop as reconnected, not an error", async () => { __setWebSocketForTest(FakeWebSocket); __setTunnelTimingForTest({ backoffMs: 5, @@ -855,18 +856,53 @@ describe("runAppSync reliability (fake WebSocket)", () => { healthy.emit({ type: "connection_ack" }); await waitUntil(() => healthy.parsed().some((m) => m.type === "subscribe")); healthy.emit({ type: "subscribe_success" }); - // Outage 2. + // A working session drops: no error yet, only the next result counts. healthy.serverClose(); + // Outage 2: one attempt after the drop fails before the handshake. await waitUntil(() => FakeWebSocket.instances.length >= 5); + const failed = FakeWebSocket.instances[4]!; + await waitUntil(() => failed.sent.length > 0); + failed.serverClose(); + // The next attempt subscribes again. + await waitUntil(() => FakeWebSocket.instances.length >= 6); + const back = FakeWebSocket.instances[5]!; + await waitUntil(() => back.sent.length > 0); + back.emit({ type: "connection_ack" }); + await waitUntil(() => back.parsed().some((m) => m.type === "subscribe")); + back.emit({ type: "subscribe_success" }); + // A drop straight back into a working session records only `reconnected`. + back.serverClose(); + await waitUntil(() => FakeWebSocket.instances.length >= 7); + const again = FakeWebSocket.instances[6]!; + await waitUntil(() => again.sent.length > 0); + again.emit({ type: "connection_ack" }); + await waitUntil(() => again.parsed().some((m) => m.type === "subscribe")); + again.emit({ type: "subscribe_success" }); + await waitUntil(() => again.parsed().some((m) => m.type === "publish")); ac.abort(); await run; - const phases = readFileSync(spoolPath(), "utf-8") + const metadata = readFileSync(spoolPath(), "utf-8") .trim() .split("\n") - .map((line) => (JSON.parse(line) as SpoolRecord).event.metadata.listen_phase); - expect(phases).toEqual(["error", "established", "error", "closed"]); + .map((line) => (JSON.parse(line) as SpoolRecord).event.metadata); + expect(metadata.map((m) => m.listen_phase)).toEqual([ + "error", + "established", + "error", + "reconnected", + "reconnected", + "closed", + ]); + expect(metadata.filter((m) => m.listen_phase === "error").map((m) => m.err_type)).toEqual([ + "listen_connect_error", + "listen_connect_error", + ]); + expect(metadata.filter((m) => m.listen_phase === "reconnected").map((m) => m.status)).toEqual([ + "ok", + "ok", + ]); } finally { restore(); __resetTelemetryRecorderForTest(); @@ -877,6 +913,108 @@ describe("runAppSync reliability (fake WebSocket)", () => { } }); + for (const [label, frame] of [ + ["publish_error", { type: "publish_error", errors: [{ message: "CONTROL_FAILED:heartbeat:404" }] }], + ["publish_success with failed", { type: "publish_success", failed: [{ message: "CONTROL_FAILED:heartbeat:404" }] }], + ] as const) { + test(`reconnects when a heartbeat finds the session expired (${label})`, async () => { + __setWebSocketForTest(FakeWebSocket); + __setTunnelTimingForTest({ backoffMs: 10, closeFlushMs: 0, idleTimeoutMs: 5000 }); + const { logs, restore } = captureLogs(); + + try { + const { ws, ac, done } = await handshake(); + ws.emit(frame); + + await waitUntil(() => FakeWebSocket.instances.length >= 2, 2000); + expect(ws.readyState).toBe(3); + expect( + logs.some((l) => l.startsWith("> Disconnected: The listen session expired. Reconnecting in")), + ).toBe(true); + expect(logs).not.toContain("> Session keepalive failed; retrying"); + + // The new socket subscribes again, which registers a fresh session. + const next = FakeWebSocket.instances[1]!; + await waitUntil(() => next.sent.length > 0); + next.emit({ type: "connection_ack" }); + await waitUntil(() => next.parsed().some((m) => m.type === "subscribe")); + expect(next.parsed().find((m) => m.type === "subscribe")?.channel).toBe( + "/webhooks/550e8400-e29b-41d4-a716-446655440000", + ); + + ac.abort(); + await done; + } finally { + restore(); + } + }); + } + + test("stops with a permanent outcome when a heartbeat is unauthorized", async () => { + __setWebSocketForTest(FakeWebSocket); + __setTunnelTimingForTest({ backoffMs: 10, closeFlushMs: 0, idleTimeoutMs: 5000 }); + const { logs, restore } = captureLogs(); + + try { + const { ws, done } = await handshake(); + ws.emit({ + type: "publish_error", + errors: [{ errorType: "UnauthorizedException", message: "denied" }], + }); + + await expect(done).resolves.toEqual({ + kind: "permanent", + reason: "Could not authorize the listen session.", + code: "APPSYNC_UNAUTHORIZED", + }); + expect(ws.readyState).toBe(3); + expect(FakeWebSocket.instances).toHaveLength(1); + expect(logs.some((l) => l.includes("Reconnecting"))).toBe(false); + } finally { + restore(); + } + }); + + test("only warns on other heartbeat failures and keeps the socket", async () => { + __setWebSocketForTest(FakeWebSocket); + __setTunnelTimingForTest({ backoffMs: 10, closeFlushMs: 0, idleTimeoutMs: 5000 }); + const { logs, restore } = captureLogs(); + + try { + const { ws, ac, done } = await handshake(); + // ESS down, its key misconfigured, or unreachable (no status): none end the session. + for (const message of [ + "CONTROL_FAILED:heartbeat:503", + "CONTROL_FAILED:heartbeat:403", + "CONTROL_FAILED:heartbeat", + ]) { + ws.emit({ type: "publish_error", errors: [{ message }] }); + } + await new Promise((r) => setTimeout(r, 30)); + + // One warning for the whole run of failures, not one per heartbeat. + const failing = () => logs.filter((l) => l === "> Session keepalive failed; retrying"); + const restored = () => logs.filter((l) => l === "> Session keepalive restored"); + expect(failing()).toHaveLength(1); + expect(restored()).toHaveLength(0); + expect(ws.readyState).toBe(1); + expect(FakeWebSocket.instances).toHaveLength(1); + + // An accepted heartbeat ends the run; a later failure warns again. + ws.emit({ type: "publish_success" }); + ws.emit({ type: "publish_success" }); + expect(restored()).toHaveLength(1); + ws.emit({ type: "publish_error", errors: [{ message: "CONTROL_FAILED:heartbeat:503" }] }); + expect(failing()).toHaveLength(2); + expect(ws.readyState).toBe(1); + + ac.abort(); + await done; + } finally { + restore(); + } + }); + test("resets backoff to the base after a session stays subscribed past the healthy threshold", async () => { __setWebSocketForTest(FakeWebSocket); __setTunnelTimingForTest({ From 83420aeb71692763b9c5b933a85dd5805a7e4f22 Mon Sep 17 00:00:00 2001 From: Muthiah Muralidharan Date: Wed, 18 Nov 2026 21:59:05 +0530 Subject: [PATCH 2/2] fix(listen): ignore publish replies once shutdown starts On Ctrl+C, listen publishes `disable` and closes the socket shortly after. A reply to `disable` in that window went through the heartbeat handling, so it could print "Session keepalive restored" or "Session keepalive failed; retrying" while shutting down. Replies after abort are now ignored. Also: - the new heartbeat tests own their AbortController before the run starts, and abort and await it in `finally` with a bounded wait, so a failing assertion no longer leaves a tunnel running; - TELEMETRY.md: `error` also covers a tunnel refused after it was up (for example, a heartbeat refused as unauthorized). Co-Authored-By: Claude Opus 5.5 --- TELEMETRY.md | 2 +- src/lib/tunnel/appsync.ts | 3 + src/tests/unit/lib/tunnel/appsync.test.ts | 82 +++++++++++++++++++---- 3 files changed, 72 insertions(+), 15 deletions(-) diff --git a/TELEMETRY.md b/TELEMETRY.md index e14e740..cb336e7 100644 --- a/TELEMETRY.md +++ b/TELEMETRY.md @@ -84,7 +84,7 @@ A telemetry request has the following structure: | `pcv` | When available | Product Catalog version (`v1` or `v2`) of the configured site. | | `code_lang` | Code generation | Generated code language, such as `go` or `js`. | | `agent` | When detected | Supported agent runtime: `claude-code`, `cursor`, `codex` (sandboxed runs) or `gemini-cli`, detected from environment variables those tools set. | -| `listen_phase` | `chargebee listen` | Tunnel lifecycle state: `established` (first connection), `reconnected` (connected again after a drop), `closed`, or `error` (a connection attempt failed). | +| `listen_phase` | `chargebee listen` | Tunnel lifecycle state: `established` (first connection), `reconnected` (connected again after a drop), `closed`, or `error` (a connection attempt failed, or the tunnel was refused and listen stopped). | | `first_run` | First recorded event only | Date (`YYYY-MM-DD`) the first-run notice was shown. | ## What is Never Collected diff --git a/src/lib/tunnel/appsync.ts b/src/lib/tunnel/appsync.ts index 9aa08dc..ff4a9ba 100644 --- a/src/lib/tunnel/appsync.ts +++ b/src/lib/tunnel/appsync.ts @@ -617,9 +617,12 @@ function connectAppSync( break; } case "publish_error": + // After abort the only publish in flight is `disable`; its reply says nothing about heartbeats. + if (signal.aborted) break; onKeepaliveFailure(msg); break; case "publish_success": + if (signal.aborted) break; if (Array.isArray(msg.failed) && msg.failed.length > 0) { onKeepaliveFailure({ errors: msg.failed }); } else if (keepaliveFailing) { diff --git a/src/tests/unit/lib/tunnel/appsync.test.ts b/src/tests/unit/lib/tunnel/appsync.test.ts index 63a46f4..3399eda 100644 --- a/src/tests/unit/lib/tunnel/appsync.test.ts +++ b/src/tests/unit/lib/tunnel/appsync.test.ts @@ -202,13 +202,18 @@ function baseOpts(over: Partial = {}): { }; } -async function handshake(): Promise<{ +/** + * Start a run and complete its handshake. Pass `controller` to own the abort + * before the run starts, so `finally` can stop it even if a wait here fails. + */ +async function handshake(controller?: AbortController): Promise<{ ws: FakeWebSocket; ac: AbortController; done: Promise; }> { FakeWebSocket.instances = []; - const { opts, ac } = baseOpts(); + const { opts, ac: own } = baseOpts(controller ? { signal: controller.signal } : {}); + const ac = controller ?? own; const done = runAppSync(opts); await waitUntil(() => (FakeWebSocket.instances[0]?.sent.length ?? 0) > 0); const ws = FakeWebSocket.instances[0]!; @@ -913,6 +918,11 @@ describe("runAppSync reliability (fake WebSocket)", () => { } }); + /** Resolve with `"pending"` if `p` hasn't settled in `ms`, so a hung run still reaches cleanup. */ + function within(p: Promise, ms = 2000): Promise { + return Promise.race([p, new Promise<"pending">((r) => setTimeout(() => r("pending"), ms))]); + } + for (const [label, frame] of [ ["publish_error", { type: "publish_error", errors: [{ message: "CONTROL_FAILED:heartbeat:404" }] }], ["publish_success with failed", { type: "publish_success", failed: [{ message: "CONTROL_FAILED:heartbeat:404" }] }], @@ -921,13 +931,16 @@ describe("runAppSync reliability (fake WebSocket)", () => { __setWebSocketForTest(FakeWebSocket); __setTunnelTimingForTest({ backoffMs: 10, closeFlushMs: 0, idleTimeoutMs: 5000 }); const { logs, restore } = captureLogs(); + const ac = new AbortController(); + let done: Promise | undefined; try { - const { ws, ac, done } = await handshake(); - ws.emit(frame); + const run = await handshake(ac); + done = run.done; + run.ws.emit(frame); await waitUntil(() => FakeWebSocket.instances.length >= 2, 2000); - expect(ws.readyState).toBe(3); + expect(run.ws.readyState).toBe(3); expect( logs.some((l) => l.startsWith("> Disconnected: The listen session expired. Reconnecting in")), ).toBe(true); @@ -941,10 +954,9 @@ describe("runAppSync reliability (fake WebSocket)", () => { expect(next.parsed().find((m) => m.type === "subscribe")?.channel).toBe( "/webhooks/550e8400-e29b-41d4-a716-446655440000", ); - - ac.abort(); - await done; } finally { + ac.abort(); + if (done) await within(done); restore(); } }); @@ -954,23 +966,28 @@ describe("runAppSync reliability (fake WebSocket)", () => { __setWebSocketForTest(FakeWebSocket); __setTunnelTimingForTest({ backoffMs: 10, closeFlushMs: 0, idleTimeoutMs: 5000 }); const { logs, restore } = captureLogs(); + const ac = new AbortController(); + let done: Promise | undefined; try { - const { ws, done } = await handshake(); - ws.emit({ + const run = await handshake(ac); + done = run.done; + run.ws.emit({ type: "publish_error", errors: [{ errorType: "UnauthorizedException", message: "denied" }], }); - await expect(done).resolves.toEqual({ + expect(await within(done)).toEqual({ kind: "permanent", reason: "Could not authorize the listen session.", code: "APPSYNC_UNAUTHORIZED", }); - expect(ws.readyState).toBe(3); + expect(run.ws.readyState).toBe(3); expect(FakeWebSocket.instances).toHaveLength(1); expect(logs.some((l) => l.includes("Reconnecting"))).toBe(false); } finally { + ac.abort(); + if (done) await within(done); restore(); } }); @@ -979,9 +996,13 @@ describe("runAppSync reliability (fake WebSocket)", () => { __setWebSocketForTest(FakeWebSocket); __setTunnelTimingForTest({ backoffMs: 10, closeFlushMs: 0, idleTimeoutMs: 5000 }); const { logs, restore } = captureLogs(); + const ac = new AbortController(); + let done: Promise | undefined; try { - const { ws, ac, done } = await handshake(); + const run = await handshake(ac); + done = run.done; + const { ws } = run; // ESS down, its key misconfigured, or unreachable (no status): none end the session. for (const message of [ "CONTROL_FAILED:heartbeat:503", @@ -1007,10 +1028,43 @@ describe("runAppSync reliability (fake WebSocket)", () => { ws.emit({ type: "publish_error", errors: [{ message: "CONTROL_FAILED:heartbeat:503" }] }); expect(failing()).toHaveLength(2); expect(ws.readyState).toBe(1); + } finally { + ac.abort(); + if (done) await within(done); + restore(); + } + }); + + test("ignores publish replies after shutdown starts (the reply to disable)", async () => { + __setWebSocketForTest(FakeWebSocket); + // Keep the socket open briefly after abort, as in production, so replies can still arrive. + __setTunnelTimingForTest({ backoffMs: 10, closeFlushMs: 50, idleTimeoutMs: 5000 }); + const { logs, restore } = captureLogs(); + const ac = new AbortController(); + let done: Promise | undefined; + + try { + const run = await handshake(ac); + done = run.done; + const { ws } = run; + ws.emit({ type: "publish_error", errors: [{ message: "CONTROL_FAILED:heartbeat:503" }] }); + expect(logs.filter((l) => l === "> Session keepalive failed; retrying")).toHaveLength(1); ac.abort(); - await done; + expect(ws.parsed().some((m) => (m.events as string[] | undefined)?.[0] === '{"op":"disable"}')).toBe(true); + // Replies to `disable` while the socket drains: none may report on heartbeats. + ws.emit({ type: "publish_success" }); + ws.emit({ type: "publish_error", errors: [{ message: "CONTROL_FAILED:disable:404" }] }); + ws.emit({ type: "publish_error", errors: [{ errorType: "UnauthorizedException" }] }); + + expect(await within(done)).toEqual({ kind: "aborted" }); + expect(logs).not.toContain("> Session keepalive restored"); + expect(logs.filter((l) => l === "> Session keepalive failed; retrying")).toHaveLength(1); + expect(logs.some((l) => l.includes("Reconnecting"))).toBe(false); + expect(FakeWebSocket.instances).toHaveLength(1); } finally { + ac.abort(); + if (done) await within(done); restore(); } });