Skip to content

Commit 99ff942

Browse files
fix(server): an interrupted relay client activation still hands the client over
Masking only the ledger write let a pending interrupt land right after it, before activeRef owned the connector, so its scope and the client stayed open: disabling left the client running and a retry started a second one. Record the client and store the connector in one uninterruptible region. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3f974f6 commit 99ff942

2 files changed

Lines changed: 59 additions & 13 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,46 @@ describe("CloudManagedEndpointRuntime", () => {
432432
}),
433433
);
434434

435+
it.effect("still owns a relay client when activation is interrupted while recording it", () =>
436+
Effect.gen(function* () {
437+
const fixture = yield* makeTransportSpawner();
438+
const recording = yield* Deferred.make<void>();
439+
const recorded = yield* Deferred.make<void>();
440+
const ledger = Layer.succeed(
441+
RelayClientLedger.RelayClientLedger,
442+
RelayClientLedger.RelayClientLedger.of({
443+
track: () =>
444+
Deferred.succeed(recording, undefined).pipe(
445+
Effect.andThen(Deferred.await(recorded)),
446+
Effect.as(Effect.void),
447+
),
448+
}),
449+
);
450+
const context = yield* Layer.build(
451+
ManagedEndpointRuntime.layer.pipe(
452+
Layer.provide(Layer.merge(runtimeDependencies(fixture.spawner), ledger)),
453+
),
454+
);
455+
const runtime = yield* Effect.service(
456+
ManagedEndpointRuntime.CloudManagedEndpointRuntime,
457+
).pipe(Effect.provide(context));
458+
459+
const activation = yield* Effect.forkChild(runtime.applyConfig(tunnelConfig));
460+
yield* Deferred.await(recording);
461+
const interrupting = yield* Effect.forkChild(Fiber.interrupt(activation), {
462+
startImmediately: true,
463+
});
464+
yield* Deferred.succeed(recorded, undefined);
465+
yield* Fiber.join(interrupting);
466+
467+
yield* runtime.applyConfig(null);
468+
expect(fixture.killed).toEqual([800]);
469+
}).pipe(
470+
Effect.provideService(HostProcess.Environment, {}),
471+
Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
472+
),
473+
);
474+
435475
it.effect("preserves an explicit transport choice despite repeated QUIC stalls", () =>
436476
Effect.gen(function* () {
437477
const fixture = yield* makeTransportSpawner({ transport: "quic" });

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -827,19 +827,25 @@ export const make = Effect.gen(function* () {
827827
}
828828

829829
if (!("status" in child)) {
830-
forgetRelayClient = yield* Effect.uninterruptible(relayClientLedger.track(Number(child.pid)));
831-
const connector = {
832-
child,
833-
executable,
834-
scope: connectorScope,
835-
configKey: nextConfigKey,
836-
config,
837-
startedAtMillis: yield* Clock.currentTimeMillis,
838-
registered: yield* Deferred.make<void>(),
839-
metricsAddress: yield* Deferred.make<string>(),
840-
protocol: yield* Ref.make<string | null>(null),
841-
} satisfies ActiveConnector;
842-
yield* Ref.set(activeRef, connector);
830+
// An interrupt before activeRef owns the connector would leave its scope, and the client, open.
831+
const connector = yield* Effect.uninterruptible(
832+
Effect.gen(function* () {
833+
forgetRelayClient = yield* relayClientLedger.track(Number(child.pid));
834+
const connector = {
835+
child,
836+
executable,
837+
scope: connectorScope,
838+
configKey: nextConfigKey,
839+
config,
840+
startedAtMillis: yield* Clock.currentTimeMillis,
841+
registered: yield* Deferred.make<void>(),
842+
metricsAddress: yield* Deferred.make<string>(),
843+
protocol: yield* Ref.make<string | null>(null),
844+
} satisfies ActiveConnector;
845+
yield* Ref.set(activeRef, connector);
846+
return connector;
847+
}),
848+
);
843849
yield* Effect.forkIn(observeConnectorOutput(connector), connectorScope);
844850
yield* Effect.forkIn(superviseConnector(connector), connectorScope);
845851
yield* Effect.forkIn(watchConnectorRegistration(connector), connectorScope);

0 commit comments

Comments
 (0)