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
46 changes: 46 additions & 0 deletions apps/desktop/src/backend/DesktopBackendPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,50 @@ describe("DesktopBackendPool", () => {
}),
),
);

it.effect("unregister stops the registered instance", () =>
Effect.scoped(
Effect.gen(function* () {
const id = DesktopBackendPool.BackendInstanceId("wsl:Ubuntu");
const pool = yield* DesktopBackendPool.DesktopBackendPool;
const instance = yield* pool.register({
id,
label: Effect.succeed("WSL (Ubuntu)"),
// A transient preflight failure leaves a restart fiber scheduled
// without needing a real child process.
configResolve: Effect.succeed({
executablePath: "/electron",
args: [],
entryPath: "/server/bin.mjs",
cwd: "/server",
env: {},
bootstrap: {
mode: "desktop",
noBrowser: true,
port: 3774,
t3Home: "/tmp/t3",
host: "127.0.0.1",
desktopBootstrapToken: "token",
tailscaleServeEnabled: false,
tailscaleServePort: 443,
},
bootstrapDelivery: "fd3",
extendEnv: true,
httpBaseUrl: new URL("http://127.0.0.1:3774"),
captureOutput: false,
preflightFailure: Option.some({ reason: "WSL starting", fatal: false }),
} satisfies DesktopBackendStartConfig),
});
yield* instance.start;
assert.equal((yield* instance.snapshot).restartScheduled, true);

yield* pool.unregister(id);

const stopped = yield* instance.snapshot;
assert.equal(stopped.desiredRunning, false);
assert.equal(stopped.restartScheduled, false);
assert.equal(Option.isNone(yield* pool.get(id)), true);
}).pipe(Effect.provide(layerPool(Ref.makeUnsafe("Windows")))),
),
);
});
8 changes: 5 additions & 3 deletions apps/desktop/src/backend/DesktopBackendPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,14 @@ export const layer = Layer.effect(
] as const);
}
return Effect.gen(function* () {
// Provide the captured factory services first, then the child scope
// last so instance finalizers are owned by the unregisterable scope.
// Provide the child scope after the captured factory context. That
// context also holds the pool layer's Scope, so providing it last
// would let it shadow the child scope and leave the instance's
// stop finalizer and restart fibers running after unregister.
const instanceScope = yield* Scope.fork(layerScope, "sequential");
const instance = yield* DesktopBackendManager.makeBackendInstance(spec).pipe(
Effect.provide(factoryContext),
Scope.provide(instanceScope),
Effect.provide(factoryContext),
);
const next = new Map(current);
next.set(spec.id, {
Expand Down
Loading