Skip to content

Commit fefb5fb

Browse files
author
PR Batch Tester
committed
fix(server): ignore superseded tunnel recovery checks
1 parent 542b7a6 commit fefb5fb

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎apps/server/src/cloud/ConnectReachability.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as NodeFS from "node:fs";
44
import { T3_CONNECT_TROUBLESHOOTING_SECTIONS } from "@t3tools/contracts";
55
import * as RelayClient from "@t3tools/shared/relayClient";
66
import { describe, expect, it } from "@effect/vitest";
7+
import * as Clock from "effect/Clock";
78
import * as Deferred from "effect/Deferred";
89
import * as Duration from "effect/Duration";
910
import * as Effect from "effect/Effect";
@@ -339,6 +340,46 @@ describe("ConnectReachability stale connector recovery", () => {
339340
}),
340341
);
341342

343+
it.effect("does not apply an older failure after a newer check resets recovery", () =>
344+
Effect.gen(function* () {
345+
const fixture = yield* makeConnectFixture();
346+
yield* fixture.runtime.applyConfig(tunnelConfig);
347+
yield* fixture.nextConnector;
348+
const clock = yield* Clock.Clock;
349+
const recoveryReached = yield* Deferred.make<void>();
350+
const releaseRecovery = yield* Deferred.make<void>();
351+
let clockReads = 0;
352+
const older = yield* fixture.reachability.check.pipe(
353+
Effect.provideService(Clock.Clock, {
354+
...clock,
355+
currentTimeMillis: Effect.suspend(() => {
356+
// The first read dates the published result; the second dates recovery.
357+
if (++clockReads !== 2) return clock.currentTimeMillis;
358+
return Deferred.succeed(recoveryReached, undefined).pipe(
359+
Effect.andThen(Deferred.await(releaseRecovery)),
360+
Effect.andThen(clock.currentTimeMillis),
361+
);
362+
}),
363+
}),
364+
Effect.forkChild({ startImmediately: true }),
365+
);
366+
yield* Deferred.await(recoveryReached);
367+
expect(Option.getOrThrow(yield* fixture.reachability.latest)).toMatchObject({
368+
cause: "no_connector",
369+
});
370+
yield* Ref.set(fixture.publicAnswer, "200");
371+
expect(yield* fixture.reachability.check).toMatchObject({ status: "reachable" });
372+
yield* TestClock.adjust(Duration.minutes(2));
373+
yield* Deferred.succeed(releaseRecovery, undefined);
374+
expect(yield* Fiber.join(older)).toMatchObject({ cause: "no_connector" });
375+
376+
yield* TestClock.adjust(Duration.minutes(2));
377+
yield* Ref.set(fixture.publicAnswer, "530");
378+
yield* fixture.reachability.check;
379+
expect(fixture.killed).toEqual([]);
380+
}),
381+
);
382+
342383
it.effect("ignores a dead-connector result once its connector is replaced or removed", () =>
343384
Effect.gen(function* () {
344385
for (const nextConfig of [{ ...tunnelConfig, connectorToken: "replacement" }, null]) {

‎apps/server/src/cloud/ConnectReachability.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ const make = Effect.gen(function* () {
277277
const restartDeadConnector = Effect.fnUntraced(function* (
278278
facts: ReachabilityFacts,
279279
result: EnvironmentConnectReachability,
280+
order: number,
280281
) {
281282
const now = yield* Clock.currentTimeMillis;
282283
const { connector } = facts;
@@ -289,6 +290,8 @@ const make = Effect.gen(function* () {
289290
: null;
290291
yield* SynchronizedRef.updateEffect(deadConnectorRef, (state) =>
291292
Effect.gen(function* () {
293+
const latest = yield* Ref.get(latestRef);
294+
if (Option.isSome(latest) && latest.value.order > order) return state;
292295
if (result.status === "reachable") {
293296
return {
294297
streak: null,
@@ -324,7 +327,7 @@ const make = Effect.gen(function* () {
324327
: [current, Option.some({ order, result })],
325328
);
326329
if (previous === null) return result;
327-
yield* restartDeadConnector(facts, result);
330+
yield* restartDeadConnector(facts, result, order);
328331
const changed = Option.match(previous, {
329332
onNone: () => true,
330333
onSome: ({ result: last }) =>

0 commit comments

Comments
 (0)