Skip to content

Commit 94ddaec

Browse files
committed
fix(connect): make replacement ownership explicit
1 parent 719b132 commit 94ddaec

1 file changed

Lines changed: 39 additions & 33 deletions

File tree

‎packages/client-runtime/src/connection/supervisor.ts‎

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
385385
);
386386
});
387387

388-
const establishScopedTracedConnection = Effect.fnUntraced(function* (
388+
const forkScopedTracedConnection = Effect.fnUntraced(function* (
389389
attempt: number,
390390
generation: number,
391391
lastFailure: ConnectionAttemptError | null,
@@ -396,22 +396,22 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
396396
return yield* Effect.uninterruptibleMask((restore) =>
397397
Effect.gen(function* () {
398398
const connectionScope = yield* Scope.fork(parentScope, "sequential");
399-
const established = yield* Effect.exit(
400-
restore(
401-
establishTracedConnection(
402-
attempt,
403-
generation,
404-
lastFailure,
405-
pendingRetry,
406-
publishProgress,
407-
).pipe(Scope.provide(connectionScope)),
399+
const fiber = yield* restore(
400+
establishTracedConnection(
401+
attempt,
402+
generation,
403+
lastFailure,
404+
pendingRetry,
405+
publishProgress,
406+
).pipe(
407+
Scope.provide(connectionScope),
408+
Effect.map(
409+
(established) =>
410+
({ ...established, scope: connectionScope }) satisfies ScopedConnection,
411+
),
408412
),
409-
);
410-
if (Exit.isFailure(established)) {
411-
yield* Scope.close(connectionScope, Exit.void).pipe(Effect.ignore);
412-
return yield* Effect.failCause(established.cause);
413-
}
414-
return { ...established.value, scope: connectionScope } satisfies ScopedConnection;
413+
).pipe(Effect.forkChild);
414+
return { fiber, scope: connectionScope };
415415
}),
416416
);
417417
});
@@ -580,15 +580,15 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
580580

581581
let failureCount = 0;
582582
for (;;) {
583-
const candidate = yield* establishScopedTracedConnection(
583+
const candidate = yield* forkScopedTracedConnection(
584584
failureCount + 1,
585585
generation,
586586
null,
587587
Option.none(),
588588
false,
589-
).pipe(Effect.forkChild);
589+
);
590590
const replacement = yield* Effect.raceAllFirst([
591-
Fiber.await(candidate).pipe(
591+
Fiber.await(candidate.fiber).pipe(
592592
Effect.map((exit): ReplacementPreparationEvent => ({ _tag: "Completed", exit })),
593593
),
594594
waitForAuthorizationDeadline(active.lease.prepared, 0).pipe(
@@ -600,11 +600,11 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
600600
]);
601601

602602
if (replacement._tag !== "Completed") {
603-
yield* Fiber.interrupt(candidate);
604-
const discarded = yield* Fiber.await(candidate);
605-
if (Exit.isSuccess(discarded)) {
606-
yield* Scope.close(discarded.value.scope, Exit.void).pipe(Effect.ignore);
607-
}
603+
yield* Fiber.interrupt(candidate.fiber);
604+
yield* Fiber.await(candidate.fiber);
605+
yield* Scope.close(candidate.scope, Exit.void).pipe(Effect.ignore);
606+
} else if (Exit.isFailure(replacement.exit)) {
607+
yield* Scope.close(candidate.scope, Exit.void).pipe(Effect.ignore);
608608
}
609609
if (replacement._tag === "AuthorizationExpired") {
610610
return Option.none<ScopedConnection>();
@@ -658,16 +658,15 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
658658
) {
659659
const initialGeneration = previousGeneration + 1;
660660
yield* SubscriptionRef.set(prepared, Option.none());
661+
const initial = yield* forkScopedTracedConnection(
662+
attempt,
663+
initialGeneration,
664+
lastFailure,
665+
pendingRetry,
666+
true,
667+
);
661668
const establishment = yield* Effect.raceAllFirst([
662-
exitUnlessInterrupted(
663-
establishScopedTracedConnection(
664-
attempt,
665-
initialGeneration,
666-
lastFailure,
667-
pendingRetry,
668-
true,
669-
),
670-
).pipe(
669+
Fiber.await(initial.fiber).pipe(
671670
Effect.map((exit): EstablishmentEvent => ({
672671
_tag: "Completed",
673672
exit,
@@ -684,6 +683,13 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
684683
),
685684
]);
686685

686+
if (establishment._tag !== "Completed") {
687+
yield* Fiber.interrupt(initial.fiber);
688+
yield* Fiber.await(initial.fiber);
689+
yield* Scope.close(initial.scope, Exit.void).pipe(Effect.ignore);
690+
} else if (Exit.isFailure(establishment.exit)) {
691+
yield* Scope.close(initial.scope, Exit.void).pipe(Effect.ignore);
692+
}
687693
if (establishment._tag === "Interrupted") {
688694
return {
689695
_tag: "Interrupted",

0 commit comments

Comments
 (0)