From 2adfc8b53034a760c9478a99702504c916dc02f7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 00:33:25 +0000 Subject: [PATCH 1/2] test(server): reproduce relay client restart after shutdown signal (#33623) These tests reproduce pingdotgg/t3code#33623: when the server receives SIGINT or SIGTERM and the relay client in the same process group exits, the supervisor restarts it because the desired config is only cleared by the runtime's finalizer later in shutdown. Co-Authored-By: Claude Opus 5.5 --- .../src/cloud/ManagedEndpointRuntime.test.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/apps/server/src/cloud/ManagedEndpointRuntime.test.ts b/apps/server/src/cloud/ManagedEndpointRuntime.test.ts index d1f95140fa0a6..2916c8f965386 100644 --- a/apps/server/src/cloud/ManagedEndpointRuntime.test.ts +++ b/apps/server/src/cloud/ManagedEndpointRuntime.test.ts @@ -955,6 +955,54 @@ describe("CloudManagedEndpointRuntime", () => { }), ); + it.effect.each(["SIGINT", "SIGTERM"] as const)( + "does not restart a connector that exits after the server receives %s", + (signal) => + Effect.gen(function* () { + const spawned: Array = []; + const firstExit = yield* Deferred.make(); + const firstKilled = yield* Deferred.make(); + const spawner = ChildProcessSpawner.make(() => + Effect.gen(function* () { + const pid = 400 + spawned.length; + spawned.push(pid); + const handle = makeHandle({ + pid, + exitCode: + pid === 400 + ? Deferred.await(firstExit) + : (Effect.never as Effect.Effect), + onKill: () => { + if (pid === 400) Deferred.doneUnsafe(firstKilled, Exit.void); + }, + }); + yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore)); + return handle; + }), + ); + // Invoke only the runtime's own listeners; emitting would reach the test worker's too. + const listenersBefore = new Set(process.listeners(signal)); + const runtime = yield* buildCloudManagedEndpointRuntime(spawner); + const runtimeListeners = process + .listeners(signal) + .filter((listener) => !listenersBefore.has(listener)); + + yield* runtime.applyConfig({ + providerKind: "cloudflare_tunnel", + connectorToken: "token", + tunnelId: "tunnel-1", + }); + for (const listener of runtimeListeners) listener(signal); + // The process group receives the signal too, so the connector exits cleanly. + yield* Deferred.succeed(firstExit, ChildProcessSpawner.ExitCode(0)); + yield* Deferred.await(firstKilled); + // Holding the reconcile lock waits out the supervisor's restart decision. + yield* runtime.restartConnector(400, Effect.succeed(true)); + + expect(spawned).toEqual([400]); + }), + ); + it.effect("serializes concurrent connector config changes", () => Effect.gen(function* () { const spawned: Array = []; From 96aec6c1622b7de69b6a7146cca82f01972b3833 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 00:37:18 +0000 Subject: [PATCH 2/2] fix(server): quitting no longer restarts the relay client mid-shutdown SIGINT and SIGTERM reach the whole process group, so the relay client exits before the runtime's finalizer clears the desired config. The supervisor then started a replacement that the finalizer stopped mid-registration, which delayed backend stop by about 2.3s. The runtime now records the shutdown signal, using the same listener pattern as ProviderSessionManager, and the supervisor skips the restart and the recovery request once the signal has arrived. Fixes pingdotgg/t3code#33623 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/cloud/ManagedEndpointRuntime.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/apps/server/src/cloud/ManagedEndpointRuntime.ts b/apps/server/src/cloud/ManagedEndpointRuntime.ts index 66ef48f547730..6e86f87ffef95 100644 --- a/apps/server/src/cloud/ManagedEndpointRuntime.ts +++ b/apps/server/src/cloud/ManagedEndpointRuntime.ts @@ -228,6 +228,22 @@ export const make = Effect.gen(function* () { const prunedRef = yield* Ref.make(false); const retryLoopRunningRef = yield* Ref.make(false); const notReadyChecksBeforeRestartRef = yield* Ref.make(CONNECTOR_NOT_READY_CHECKS_BEFORE_RESTART); + // The relay client shares the server's process group, so it exits on these signals before the finalizer runs. + let shutdownSignalReceived = false; + const onShutdownSignal = () => { + shutdownSignalReceived = true; + }; + yield* Effect.acquireRelease( + Effect.sync(() => { + process.on("SIGINT", onShutdownSignal); + process.on("SIGTERM", onShutdownSignal); + }), + () => + Effect.sync(() => { + process.off("SIGINT", onShutdownSignal); + process.off("SIGTERM", onShutdownSignal); + }), + ); // An explicit cloudflared transport in the environment is the user's choice. const env = yield* HostProcess.Environment; const transportChosenByUser = Option.isSome( @@ -363,6 +379,7 @@ export const make = Effect.gen(function* () { const desiredConfig = yield* Ref.get(desiredConfigRef); if ( + shutdownSignalReceived || !desiredConfig || desiredConfig.providerKind !== "cloudflare_tunnel" || runtimeConfigKey(desiredConfig) !== connector.configKey