Skip to content

Commit ff02570

Browse files
committed
fix(desktop): detect dead attached servers and skip WSL auto-attach
Watch attached external backends for liveness and readiness so a stopped server clears its external state and triggers recovery. Restrict auto-attach to native primaries so a WSL exit cannot attach the Windows environment, and rename the node:crypto import to satisfy lint.
1 parent 55c51e0 commit ff02570

7 files changed

Lines changed: 424 additions & 30 deletions

File tree

‎apps/desktop/src/backend/DesktopAutoAttach.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe("DesktopAutoAttach", () => {
5353
origin: "http://127.0.0.1:4545",
5454
port: 4545,
5555
startedAt: "2026-01-01T00:00:00.000Z",
56+
desktopBootstrapSecret: "deadbeef00112233445566778899aabb",
5657
});
5758

5859
let attached = false;

‎apps/desktop/src/backend/DesktopBackendConfiguration.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ describe("DesktopBackendConfiguration", () => {
268268
),
269269
);
270270
assert.equal(
271-
yield* configuration.bootstrapTokenForSecret(first.bootstrap.desktopBootstrapSecret ?? ""),
271+
yield* configuration.bootstrapTokenForSecret(
272+
first.bootstrap.desktopBootstrapSecret ?? "",
273+
),
272274
first.bootstrap.desktopBootstrapToken,
273275
);
274276
}),

‎apps/desktop/src/backend/DesktopBackendManager.test.ts‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,13 +627,13 @@ describe("DesktopBackendManager", () => {
627627
httpBaseUrl: new URL("http://127.0.0.1:4545"),
628628
};
629629

630-
yield* instance.attachExternal(externalConfig, Option.some(777));
630+
yield* instance.attachExternal(externalConfig, Option.some(process.pid));
631631

632632
const attached = yield* instance.snapshot;
633633
assert.isTrue(attached.ready);
634634
assert.isTrue(attached.desiredRunning);
635635
assert.isTrue(attached.isExternal);
636-
assert.deepEqual(attached.activePid, Option.some(777));
636+
assert.deepEqual(attached.activePid, Option.some(process.pid));
637637
assert.deepEqual(yield* instance.currentConfig, Option.some(externalConfig));
638638
assert.deepEqual(
639639
readyUrls.map((url) => url.href),
@@ -653,6 +653,62 @@ describe("DesktopBackendManager", () => {
653653
),
654654
);
655655

656+
it.effect("invalidates external attachment and recovers when the attached server stops", () =>
657+
Effect.scoped(
658+
Effect.gen(function* () {
659+
let externalHealthy = true;
660+
let spawnCount = 0;
661+
let shutdownCount = 0;
662+
const instance = yield* makeTestInstance({
663+
spawnerLayer: Layer.succeed(
664+
ChildProcessSpawner.ChildProcessSpawner,
665+
ChildProcessSpawner.make(() =>
666+
Effect.sync(() => {
667+
spawnCount += 1;
668+
return makeProcess({ exitCode: Effect.never });
669+
}),
670+
),
671+
),
672+
httpClientLayer: layerHttpClient((request) =>
673+
Effect.sync(() => {
674+
if (request.url.startsWith("http://127.0.0.1:4545/")) {
675+
return responseForRequest(request, externalHealthy ? 200 : 503);
676+
}
677+
return responseForRequest(request, 200);
678+
}),
679+
),
680+
onShutdown: Effect.sync(() => {
681+
shutdownCount += 1;
682+
}),
683+
});
684+
const externalConfig = {
685+
...baseConfig,
686+
httpBaseUrl: new URL("http://127.0.0.1:4545"),
687+
};
688+
689+
yield* instance.attachExternal(externalConfig, Option.some(process.pid));
690+
assert.isTrue((yield* instance.snapshot).ready);
691+
assert.isTrue((yield* instance.snapshot).isExternal);
692+
693+
externalHealthy = false;
694+
yield* TestClock.adjust(Duration.seconds(1));
695+
696+
const afterLoss = yield* instance.snapshot;
697+
assert.isFalse(afterLoss.ready);
698+
assert.isUndefined(afterLoss.isExternal);
699+
assert.deepEqual(afterLoss.activePid, Option.none());
700+
assert.isTrue(afterLoss.restartScheduled);
701+
assert.equal(shutdownCount, 1);
702+
703+
yield* TestClock.adjust(Duration.millis(500));
704+
assert.equal(spawnCount, 1);
705+
const recovered = yield* instance.snapshot;
706+
assert.isTrue(recovered.ready);
707+
assert.deepEqual(recovered.activePid, Option.some(123));
708+
}).pipe(Effect.provide(TestClock.layer())),
709+
),
710+
);
711+
656712
it.effect("reports state-directory ownership once without scheduling another backend", () =>
657713
Effect.scoped(
658714
Effect.gen(function* () {

‎apps/desktop/src/backend/DesktopBackendManager.ts‎

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as Brand from "effect/Brand";
22
import * as Cause from "effect/Cause";
3+
import * as Context from "effect/Context";
34
import * as Duration from "effect/Duration";
45
import * as Effect from "effect/Effect";
56
import * as Exit from "effect/Exit";
@@ -13,7 +14,7 @@ import * as Schema from "effect/Schema";
1314
import * as Semaphore from "effect/Semaphore";
1415
import * as Scope from "effect/Scope";
1516
import * as Stream from "effect/Stream";
16-
import { HttpClient } from "effect/http";
17+
import { HttpClient, HttpClientRequest } from "effect/http";
1718
import { ChildProcess, ChildProcessSpawner } from "effect/process";
1819

1920
import {
@@ -39,6 +40,7 @@ const MAX_PREFLIGHT_FAILURE_ATTEMPTS = 5;
3940
const DEFAULT_BACKEND_READINESS_TIMEOUT = Duration.minutes(3);
4041
const DEFAULT_BACKEND_READINESS_INTERVAL = Duration.millis(100);
4142
const DEFAULT_BACKEND_READINESS_REQUEST_TIMEOUT = Duration.seconds(1);
43+
const EXTERNAL_LIVENESS_INTERVAL = Duration.seconds(1);
4244
// Match the service launcher's grace period so backends can stop provider sessions.
4345
const DEFAULT_BACKEND_TERMINATE_GRACE = Duration.seconds(5);
4446
const DEFAULT_BACKEND_OUTPUT_DRAIN_TIMEOUT = Duration.seconds(5);
@@ -49,6 +51,30 @@ const STARTUP_FAILURE_OUTPUT_CHARS = 4_000;
4951
const { logWarning: logBackendProcessWarning } =
5052
DesktopObservability.makeComponentLogger("desktop-backend-process");
5153

54+
const isExternalPidAlive = (pid: number): boolean => {
55+
if (pid <= 0) {
56+
return false;
57+
}
58+
try {
59+
process.kill(pid, 0);
60+
return true;
61+
} catch (error) {
62+
return (
63+
typeof error === "object" &&
64+
error !== null &&
65+
"code" in error &&
66+
(error.code === "EPERM" || error.code === "EACCES")
67+
);
68+
}
69+
};
70+
71+
const ProcessLivenessProbe = Context.Reference<(pid: number) => Effect.Effect<boolean>>(
72+
"@t3tools/desktop/backend/ProcessLivenessProbe",
73+
{
74+
defaultValue: () => (pid: number) => Effect.sync(() => isExternalPidAlive(pid)),
75+
},
76+
);
77+
5278
type BackendProcessLayerServices = ChildProcessSpawner.ChildProcessSpawner | HttpClient.HttpClient;
5379

5480
type BackendProcessRunRequirements = BackendProcessLayerServices | Scope.Scope;
@@ -278,6 +304,7 @@ interface BackendManagerState {
278304
readonly nextRunId: number;
279305
readonly isExternal?: boolean;
280306
readonly externalPid?: Option.Option<number>;
307+
readonly externalWatcherFiber: Option.Option<Fiber.Fiber<void, never>>;
281308
}
282309

283310
const initialState: BackendManagerState = {
@@ -291,6 +318,7 @@ const initialState: BackendManagerState = {
291318
nextRunId: 1,
292319
isExternal: false,
293320
externalPid: Option.none(),
321+
externalWatcherFiber: Option.none(),
294322
};
295323

296324
const activePid = (active: Option.Option<ActiveBackendRun>): Option.Option<number> =>
@@ -689,6 +717,21 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
689717
});
690718
});
691719

720+
const cancelExternalWatcher = Effect.gen(function* () {
721+
const externalWatcherFiber = yield* SubscriptionRef.modify(state, (current) => [
722+
current.externalWatcherFiber,
723+
{
724+
...current,
725+
externalWatcherFiber: Option.none(),
726+
},
727+
]);
728+
729+
yield* Option.match(externalWatcherFiber, {
730+
onNone: () => Effect.void,
731+
onSome: (fiber) => Fiber.interrupt(fiber).pipe(Effect.asVoid),
732+
});
733+
});
734+
692735
const start: Effect.Effect<void> = Effect.suspend(() =>
693736
mutex.withPermits(1)(
694737
Effect.gen(function* () {
@@ -740,6 +783,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
740783
const resetFatalPreflightCounter =
741784
!current.desiredRunning && current.preflightFailureAttempt > 0;
742785
yield* cancelRestart;
786+
yield* cancelExternalWatcher;
743787
yield* SubscriptionRef.update(state, (latest) => ({
744788
...latest,
745789
desiredRunning: true,
@@ -748,6 +792,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
748792
preflightFailureAttempt: resetFatalPreflightCounter ? 0 : latest.preflightFailureAttempt,
749793
isExternal: false,
750794
externalPid: Option.none<number>(),
795+
externalWatcherFiber: Option.none(),
751796
}));
752797

753798
const preflightFailure = config.value.preflightFailure;
@@ -1076,7 +1121,9 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
10761121
const stop = Effect.fn("desktop.backendInstance.stop")(function* (options?: {
10771122
readonly timeout?: Duration.Duration;
10781123
}) {
1079-
const { active, restartFiber, notifyShutdown } = yield* mutex.withPermits(1)(
1124+
const { active, restartFiber, externalWatcherFiber, notifyShutdown } = yield* mutex.withPermits(
1125+
1,
1126+
)(
10801127
Effect.gen(function* () {
10811128
const result = yield* SubscriptionRef.modify(state, (latest) => {
10821129
const active = Option.map(latest.active, (run) =>
@@ -1086,6 +1133,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
10861133
{
10871134
active,
10881135
restartFiber: latest.restartFiber,
1136+
externalWatcherFiber: latest.externalWatcherFiber,
10891137
notifyShutdown: latest.ready,
10901138
},
10911139
{
@@ -1094,6 +1142,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
10941142
ready: false,
10951143
active,
10961144
restartFiber: Option.none<Fiber.Fiber<void, never>>(),
1145+
externalWatcherFiber: Option.none<Fiber.Fiber<void, never>>(),
10971146
isExternal: false,
10981147
externalPid: Option.none<number>(),
10991148
},
@@ -1110,6 +1159,10 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
11101159
onNone: () => Effect.void,
11111160
onSome: (fiber) => Fiber.interrupt(fiber).pipe(Effect.asVoid),
11121161
});
1162+
yield* Option.match(externalWatcherFiber, {
1163+
onNone: () => Effect.void,
1164+
onSome: (fiber) => Fiber.interrupt(fiber).pipe(Effect.asVoid),
1165+
});
11131166
yield* Option.match(active, {
11141167
onNone: () => Effect.void,
11151168
// The enclosing operation owns the span for this inline handler.
@@ -1167,11 +1220,90 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
11671220
});
11681221
});
11691222

1223+
const probeHttpReady = Effect.fn("desktop.backendInstance.probeHttpReady")(function* (
1224+
httpBaseUrl: URL,
1225+
) {
1226+
const readinessUrl = new URL(BACKEND_READINESS_PATH, httpBaseUrl).toString();
1227+
const isReady = yield* httpClient.execute(HttpClientRequest.get(readinessUrl)).pipe(
1228+
Effect.timeoutOption(DEFAULT_BACKEND_READINESS_REQUEST_TIMEOUT),
1229+
Effect.flatMap((responseOption) =>
1230+
Option.match(responseOption, {
1231+
onNone: () => Effect.succeed(false),
1232+
onSome: (res) => Effect.succeed(res.status >= 200 && res.status < 300),
1233+
}),
1234+
),
1235+
Effect.orElseSucceed(() => false),
1236+
);
1237+
return isReady;
1238+
});
1239+
1240+
const watchExternal = Effect.fn("desktop.backendInstance.watchExternal")(function* (
1241+
config: DesktopBackendStartConfig,
1242+
pid: Option.Option<number>,
1243+
) {
1244+
const isProcessAlive = yield* ProcessLivenessProbe;
1245+
while (true) {
1246+
yield* Effect.sleep(EXTERNAL_LIVENESS_INTERVAL);
1247+
const current = yield* SubscriptionRef.get(state);
1248+
if (!current.isExternal || !current.ready) {
1249+
return;
1250+
}
1251+
1252+
const pidAlive = yield* Option.match(pid, {
1253+
onSome: (p) => isProcessAlive(p),
1254+
onNone: () => Effect.succeed(true),
1255+
});
1256+
1257+
const isAlive = pidAlive ? yield* probeHttpReady(config.httpBaseUrl) : false;
1258+
1259+
if (!isAlive) {
1260+
yield* logInstanceWarning("attached external backend is no longer running");
1261+
yield* mutex.withPermits(1)(
1262+
Effect.gen(function* () {
1263+
const { shouldRecover, wasReady } = yield* SubscriptionRef.modify(
1264+
state,
1265+
(
1266+
latest,
1267+
): readonly [
1268+
{ readonly shouldRecover: boolean; readonly wasReady: boolean },
1269+
BackendManagerState,
1270+
] => {
1271+
if (!latest.isExternal || !latest.ready) {
1272+
return [{ shouldRecover: false, wasReady: false }, latest];
1273+
}
1274+
return [
1275+
{ shouldRecover: latest.desiredRunning, wasReady: latest.ready },
1276+
{
1277+
...latest,
1278+
ready: false,
1279+
isExternal: false,
1280+
externalPid: Option.none(),
1281+
externalWatcherFiber: Option.none(),
1282+
},
1283+
];
1284+
},
1285+
);
1286+
1287+
if (wasReady) {
1288+
yield* (spec.onShutdown?.() ?? Effect.void).pipe(Effect.ignore);
1289+
}
1290+
1291+
if (shouldRecover) {
1292+
yield* scheduleRestart("attached external server exited");
1293+
}
1294+
}),
1295+
);
1296+
return;
1297+
}
1298+
}
1299+
});
1300+
11701301
const attachExternal = Effect.fn("desktop.backendInstance.attachExternal")(function* (
11711302
config: DesktopBackendStartConfig,
11721303
pid: Option.Option<number> = Option.none(),
11731304
) {
11741305
yield* cancelRestart;
1306+
yield* cancelExternalWatcher;
11751307
yield* SubscriptionRef.update(state, (latest) => ({
11761308
...latest,
11771309
desiredRunning: true,
@@ -1181,8 +1313,25 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
11811313
restartAttempt: 0,
11821314
isExternal: true,
11831315
externalPid: pid,
1316+
externalWatcherFiber: Option.none(),
11841317
}));
11851318
yield* (spec.onReady?.(config.httpBaseUrl) ?? Effect.void).pipe(Effect.ignore);
1319+
1320+
const watcherFiber = yield* Effect.forkIn(
1321+
watchExternal(config, pid).pipe(
1322+
Effect.catchCause((cause) =>
1323+
logInstanceError("desktop backend external watcher failed", {
1324+
cause: Cause.pretty(cause),
1325+
}),
1326+
),
1327+
),
1328+
parentScope,
1329+
);
1330+
yield* SubscriptionRef.update(state, (latest) =>
1331+
latest.isExternal && Option.isNone(latest.externalWatcherFiber)
1332+
? { ...latest, externalWatcherFiber: Option.some(watcherFiber) }
1333+
: latest,
1334+
);
11861335
});
11871336

11881337
const waitForReady = Effect.fn("DesktopBackendManager.waitForReady")(

0 commit comments

Comments
 (0)