Skip to content

Commit 9ebdb0f

Browse files
t3dotggclaude
andcommitted
fix(server): recover when the old connector exited during a failed swap
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7ac0b5b commit 9ebdb0f

2 files changed

Lines changed: 67 additions & 4 deletions

File tree

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,65 @@ describe("CloudManagedEndpointRuntime", () => {
11931193
}),
11941194
);
11951195

1196+
it.effect("requests recovery when the older connector exited during a failed swap", () =>
1197+
Effect.gen(function* () {
1198+
const olderClient = {
1199+
status: "available",
1200+
executablePath: "/managed/2025.9.0/cloudflared",
1201+
source: "managed",
1202+
version: "2025.9.0",
1203+
} as const;
1204+
const pinnedClient = {
1205+
status: "available",
1206+
executablePath: "/managed/pinned/cloudflared",
1207+
source: "managed",
1208+
version: RelayClient.CLOUDFLARED_VERSION,
1209+
} as const;
1210+
let installed = false;
1211+
const releaseInstall = yield* Deferred.make<void>();
1212+
const spawner = ChildProcessSpawner.make((command) =>
1213+
Effect.gen(function* () {
1214+
const executable = ChildProcess.isStandardCommand(command) ? command.command : "";
1215+
if (executable === pinnedClient.executablePath) {
1216+
return yield* PlatformError.systemError({
1217+
_tag: "PermissionDenied",
1218+
module: "ChildProcess",
1219+
method: "spawn",
1220+
});
1221+
}
1222+
// The older connector has exited by the time the swap fails.
1223+
const handle = makeHandle({ pid: 12, onKill: () => {}, isRunning: () => !installed });
1224+
yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
1225+
return handle;
1226+
}),
1227+
);
1228+
const runtime = yield* buildCloudManagedEndpointRuntime(
1229+
spawner,
1230+
Layer.succeed(
1231+
RelayClient.RelayClient,
1232+
RelayClient.RelayClient.of({
1233+
resolve: Effect.sync(() => (installed ? pinnedClient : olderClient)),
1234+
install: Deferred.await(releaseInstall).pipe(
1235+
Effect.andThen(
1236+
Effect.sync(() => {
1237+
installed = true;
1238+
return pinnedClient;
1239+
}),
1240+
),
1241+
),
1242+
installWithProgress: () => Effect.die("unused"),
1243+
pruneManagedVersions: Effect.void,
1244+
}),
1245+
),
1246+
);
1247+
const config = { providerKind: "cloudflare_tunnel", connectorToken: "token" } as const;
1248+
expect(yield* runtime.applyConfig(config)).toMatchObject({ status: "running", pid: 12 });
1249+
1250+
yield* Deferred.succeed(releaseInstall, undefined);
1251+
expect(Option.getOrThrow(yield* Stream.runHead(runtime.recoveryRequests))).toEqual(config);
1252+
}),
1253+
);
1254+
11961255
it.effect("restarts a self-updated pinned-folder connector once the pin is reinstalled", () =>
11971256
Effect.gen(function* () {
11981257
const executablePath = "/managed/pinned/cloudflared";

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,16 @@ export const make = Effect.gen(function* () {
299299
const next = yield* Ref.get(activeRef);
300300
if (!next) {
301301
yield* Effect.logWarning("Relay client did not start after the update", status);
302-
if (previous) {
303-
// The old connector still serves; the retry loop tries the pin again.
302+
// A still-running old connector keeps serving and the retry loop
303+
// tries the pin again. One that exited while detached was skipped
304+
// by its supervisor, so ask for recovery like an exited connector.
305+
const previousRunning = previous
306+
? yield* previous.child.isRunning.pipe(Effect.orElseSucceed(() => false))
307+
: false;
308+
if (previous && previousRunning) {
304309
yield* Ref.set(activeRef, previous);
305310
} else {
306-
// No supervisor watches a failed spawn, so ask for recovery like
307-
// an exited connector would.
311+
yield* stopConnector(previous);
308312
yield* Queue.offer(recoveryRequests, desiredConfig);
309313
}
310314
return;

0 commit comments

Comments
 (0)