Fix backend shutdown edge cases

This commit is contained in:
Josh Gratton committed 2026-06-01 18:39:30 -04:00
1 parent c61ea48549
commit d84971a0f9
3 files changed
+41 -2

No files matched your search

+2 -1
View File
@@ -1,3 +1,4 @@
import { PRIMARY_LOCAL_ENVIRONMENT_ID } from "@t3tools/contracts";
import { makeLocalFileTracer, makeTraceSink } from "@t3tools/shared/observability";
import { parsePersistedServerObservabilitySettings } from "@t3tools/shared/serverSettings";
import * as Context from "effect/Context";
@@ -308,7 +309,7 @@ const writeBackendChildLogRecord = Effect.fn("desktop.observability.writeBackend
},
);
const PRIMARY_BACKEND_LOG_INSTANCE_ID = "primary";
const PRIMARY_BACKEND_LOG_INSTANCE_ID = PRIMARY_LOCAL_ENVIRONMENT_ID;
const sanitizeInstanceIdForFileName = (id: string): string => id.replace(/[^a-zA-Z0-9._-]+/g, "_");
@@ -338,6 +338,42 @@ describe("DesktopBackendManager", () => {
),
);
it.effect("does not notify shutdown before the first start has prior state", () =>
Effect.scoped(
Effect.gen(function* () {
let shutdownCount = 0;
const closed = yield* Deferred.make<void>();
const startedPids = yield* Queue.unbounded<number>();
const spawnerLayer = Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make(() =>
Effect.gen(function* () {
yield* Queue.offer(startedPids, 123);
const close = Deferred.succeed(closed, void 0).pipe(Effect.asVoid);
return makeProcess({
exitCode: Deferred.await(closed).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
kill: () => close,
});
}),
),
);
const instance = yield* makeTestInstance({
spawnerLayer,
httpClientLayer: httpClientLayer(() => Effect.never),
onShutdown: Effect.sync(() => {
shutdownCount += 1;
}),
});
yield* instance.start;
assert.equal(yield* Queue.take(startedPids), 123);
assert.equal(shutdownCount, 0);
}),
),
);
it.effect("restarts an unexpectedly exited backend with the Effect clock", () =>
Effect.scoped(
Effect.gen(function* () {
@@ -408,7 +408,9 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
return;
}
yield* spec.onShutdown?.() ?? Effect.void;
if (current.ready || Option.isSome(current.config)) {
yield* spec.onShutdown?.() ?? Effect.void;
}
const config = yield* spec.configResolve.pipe(
Effect.tapError((error) =>
logInstanceError("failed to generate desktop backend configuration", {