The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 00:45 UTC
fix(server): quitting no longer restarts the relay client mid-shutdown (#33628)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 10, 2026 at 00:00 UTC
5636b5666d3fb9b2b280dbe8a0c5cb42874f1bcd
2 files changed
+65
apps/server/src/cloud/ManagedEndpointRuntime.test.ts
+48
@@ -955,6 +955,54 @@ describe("CloudManagedEndpointRuntime", () => {
955
}),
956
);
957
958
+ it.effect.each(["SIGINT", "SIGTERM"] as const)(
959
+ "does not restart a connector that exits after the server receives %s",
960
+ (signal) =>
961
+ Effect.gen(function* () {
962
+ const spawned: Array<number> = [];
963
+ const firstExit = yield* Deferred.make<ChildProcessSpawner.ExitCode>();
964
+ const firstKilled = yield* Deferred.make<void>();
965
+ const spawner = ChildProcessSpawner.make(() =>
966
+ Effect.gen(function* () {
967
+ const pid = 400 + spawned.length;
968
+ spawned.push(pid);
969
+ const handle = makeHandle({
970
+ pid,
971
+ exitCode:
972
+ pid === 400
973
+ ? Deferred.await(firstExit)
974
+ : (Effect.never as Effect.Effect<ChildProcessSpawner.ExitCode>),
975
+ onKill: () => {
976
+ if (pid === 400) Deferred.doneUnsafe(firstKilled, Exit.void);
977
+ },
978
+ });
979
+ yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
980
+ return handle;
981
+ }),
982
+ );
983
+ // Invoke only the runtime's own listeners; emitting would reach the test worker's too.
984
+ const listenersBefore = new Set(process.listeners(signal));
985
+ const runtime = yield* buildCloudManagedEndpointRuntime(spawner);
986
+ const runtimeListeners = process
987
+ .listeners(signal)
988
+ .filter((listener) => !listenersBefore.has(listener));
989
+
990
+ yield* runtime.applyConfig({
991
+ providerKind: "cloudflare_tunnel",
992
+ connectorToken: "token",
993
+ tunnelId: "tunnel-1",
994
+ });
995
+ for (const listener of runtimeListeners) listener(signal);
996
+ // The process group receives the signal too, so the connector exits cleanly.
997
+ yield* Deferred.succeed(firstExit, ChildProcessSpawner.ExitCode(0));
998
+ yield* Deferred.await(firstKilled);
999
+ // Holding the reconcile lock waits out the supervisor's restart decision.
1000
+ yield* runtime.restartConnector(400, Effect.succeed(true));
1001
+
1002
+ expect(spawned).toEqual([400]);
1003
+ }),
1004
+ );
1005
+
1006
it.effect("serializes concurrent connector config changes", () =>
1007
Effect.gen(function* () {
1008
const spawned: Array<number> = [];
apps/server/src/cloud/ManagedEndpointRuntime.ts
+17
@@ -228,6 +228,22 @@ export const make = Effect.gen(function* () {
228
const prunedRef = yield* Ref.make(false);
229
const retryLoopRunningRef = yield* Ref.make(false);
230
const notReadyChecksBeforeRestartRef = yield* Ref.make(CONNECTOR_NOT_READY_CHECKS_BEFORE_RESTART);
231
+ // The relay client shares the server's process group, so it exits on these signals before the finalizer runs.
232
+ let shutdownSignalReceived = false;
233
+ const onShutdownSignal = () => {
234
+ shutdownSignalReceived = true;
235
+ };
236
+ yield* Effect.acquireRelease(
237
+ Effect.sync(() => {
238
+ process.on("SIGINT", onShutdownSignal);
239
+ process.on("SIGTERM", onShutdownSignal);
240
+ }),
241
+ () =>
242
+ Effect.sync(() => {
243
+ process.off("SIGINT", onShutdownSignal);
244
+ process.off("SIGTERM", onShutdownSignal);
245
+ }),
246
+ );
247
// An explicit cloudflared transport in the environment is the user's choice.
248
const env = yield* HostProcess.Environment;
249
const transportChosenByUser = Option.isSome(
@@ -363,6 +379,7 @@ export const make = Effect.gen(function* () {
379
380
const desiredConfig = yield* Ref.get(desiredConfigRef);
381
if (
382
+ shutdownSignalReceived ||
383
!desiredConfig ||
384
desiredConfig.providerKind !== "cloudflare_tunnel" ||
385
runtimeConfigKey(desiredConfig) !== connector.configKey