diff --git a/packages/client/src/daemon-client.test.ts b/packages/client/src/daemon-client.test.ts index 277e5b9042..1a8a4ee144 100644 --- a/packages/client/src/daemon-client.test.ts +++ b/packages/client/src/daemon-client.test.ts @@ -709,6 +709,13 @@ class FakeDaemon { ); } + socketFailsOnHello(): void { + this.transport.send = () => { + throw new Error("WebSocket not open"); + }; + this.onOpen(); + } + daemonAnswersPingsAfter(delay: "fast" | `${number}s`): void { this.pongMode = { kind: "answer", @@ -2819,6 +2826,33 @@ test("connection timeout includes credential lookup", async () => { expect(await outcome).toMatchObject({ message: "Connection timed out" }); }); +test("a failed hello retires the deadline of the foreground replacement", async () => { + useHeartbeatClock(); + const stalled = new FakeDaemon(); + const closing = new FakeDaemon(); + const healthy = new FakeDaemon(); + const { client, attempts } = connectThroughReplacements( + "foreground-failed-hello", + [stalled, closing, healthy], + { connectTimeoutMs: 4_000 }, + ); + const connection = client.connect(); + void connection.catch(() => undefined); + await vi.advanceTimersByTimeAsync(3_100); + client.ensureConnected({ verify: true }); + expect(attempts()).toBe(2); + await vi.advanceTimersByTimeAsync(3_900); + closing.socketFailsOnHello(); + + await vi.advanceTimersByTimeAsync(1_499); + expect(client.lastError).toBe("WebSocket not open"); + expect(attempts()).toBe(2); + await vi.advanceTimersByTimeAsync(1); + expect(attempts()).toBe(3); + healthy.openConnection(); + await connection; +}); + test("foreground keeps a connect attempt that just started", async () => { useHeartbeatClock(); const pending = new FakeDaemon(); @@ -2995,14 +3029,14 @@ test("chunk progress beyond the daemon lease keeps sending heartbeat pings", asy function connectThroughReplacements( clientId: string, daemons: FakeDaemon[], - options?: { reconnect?: { enabled: boolean } }, + options?: { reconnect?: { enabled: boolean }; connectTimeoutMs?: number }, ): { client: DaemonClient; attempts: () => number } { let attempts = 0; const client = new DaemonClient({ url: "ws://test", clientId, logger: noopLogger, - ...(options?.reconnect ? { reconnect: options.reconnect } : {}), + ...options, transportFactory: () => { const daemon = daemons[Math.min(attempts, daemons.length - 1)]!; attempts += 1; diff --git a/packages/client/src/daemon-client.ts b/packages/client/src/daemon-client.ts index f41279123b..04a7c4a690 100644 --- a/packages/client/src/daemon-client.ts +++ b/packages/client/src/daemon-client.ts @@ -1701,7 +1701,6 @@ export class DaemonClient { event && typeof event === "object" && "code" in event ? event.code : "unknown", ), }); - this.clearConnectionAttempt(); if (this.pendingGenericTransportErrorTimeout) { clearTimeout(this.pendingGenericTransportErrorTimeout); this.pendingGenericTransportErrorTimeout = null; @@ -1780,7 +1779,6 @@ export class DaemonClient { ]; } catch (error) { if (this.connectionAttempt !== attempt) return; - this.clearConnectionAttempt(); const message = error instanceof Error ? error.message : "Failed to connect"; this.lastErrorValue = message; this.scheduleReconnect({ @@ -7872,6 +7870,7 @@ export class DaemonClient { // Attempt now instead of after the backoff delay. Failures of that attempt back off as usual. immediate?: boolean; }): void { + this.clearConnectionAttempt(); if (this.reconnectTimeout) { clearTimeout(this.reconnectTimeout); this.reconnectTimeout = null;