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 = []; 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