Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions apps/server/src/cloud/ManagedEndpointRuntime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> = [];
const firstExit = yield* Deferred.make<ChildProcessSpawner.ExitCode>();
const firstKilled = yield* Deferred.make<void>();
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<ChildProcessSpawner.ExitCode>),
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<number> = [];
Expand Down
17 changes: 17 additions & 0 deletions apps/server/src/cloud/ManagedEndpointRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
Loading