@devries-cameron20 / t3code-1 / commits / da3f97299f
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): startup no longer hangs on a shell stuck in the kernel (#27938)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>

AdEx-Partners-DE committed Nov 4, 2026 at 00:04 UTC da3f97299f258eccf5ae80e7b6b3391ea7376ed8
4 files changed +85 -3
apps/desktop/src/shell/DesktopShellEnvironment.test.ts
+37
@@ -3,6 +3,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
3 import { assert, describe, it } from "@effect/vitest";
4 import * as HostProcess from "@t3tools/shared/HostProcess";
5 import * as Effect from "effect/Effect";
6 +import * as Fiber from "effect/Fiber";
7 import * as Deferred from "effect/Deferred";
8 import * as FileSystem from "effect/FileSystem";
9 import * as Layer from "effect/Layer";
@@ -14,6 +15,7 @@ import * as Schema from "effect/Schema";
15 import * as Option from "effect/Option";
16 import * as ChildProcess from "effect/process/ChildProcess";
17 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
18 +import * as TestClock from "effect/testing/TestClock";
19 import * as NodeOS from "node:os";
20 import { vi } from "vite-plus/test";
21
@@ -60,6 +62,22 @@ function makeProcess(output: string, exitCode = 0): ChildProcessSpawner.ChildPro
62 });
63 }
64
65 +function makeHungProcess(): ChildProcessSpawner.ChildProcessHandle {
66 + return ChildProcessSpawner.makeHandle({
67 + pid: ChildProcessSpawner.ProcessId(124),
68 + stdout: Stream.never,
69 + stderr: Stream.never,
70 + all: Stream.never,
71 + exitCode: Effect.never,
72 + isRunning: Effect.succeed(true),
73 + kill: () => Effect.never,
74 + stdin: Sink.drain,
75 + getInputFd: () => Sink.drain,
76 + getOutputFd: () => Stream.empty,
77 + unref: Effect.succeed(Effect.void),
78 + });
79 +}
80 +
81 function withProcessEnv<A, E, R>(
82 env: NodeJS.ProcessEnv,
83 effect: Effect.Effect<A, E, R>,
@@ -673,6 +691,25 @@ describe("DesktopShellEnvironment", () => {
691 }),
692 );
693
694 + it.effect("gives up on a Windows shell that survives the kill", () =>
695 + Effect.gen(function* () {
696 + const commands: Array<string> = [];
697 + const fiber = yield* runShellEnvironment({
698 + env: { PATH: "C:\\Windows", APPDATA: "C:\\Roaming" },
699 + platform: "win32",
700 + handler: () => "",
701 + processSpawner: ChildProcessSpawner.make((command) => {
702 + commands.push(command._tag === "StandardCommand" ? command.command : "");
703 + return Effect.acquireRelease(Effect.succeed(makeHungProcess()), () => Effect.never);
704 + }),
705 + }).pipe(Effect.forkChild({ startImmediately: true }));
706 +
707 + for (let probe = 0; probe < 4; probe++) yield* TestClock.adjust("5 seconds");
708 + yield* Fiber.join(fiber);
709 + assert.deepEqual([...new Set(commands)], ["pwsh.exe", "powershell.exe"]);
710 + }),
711 + );
712 +
713 it.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () =>
714 Effect.gen(function* () {
715 const env: NodeJS.ProcessEnv = { Path: "C:\\Windows\\System32" };
apps/desktop/src/shell/DesktopShellEnvironment.ts
+7 -1
@@ -8,6 +8,7 @@ import {
8 import * as Context from "effect/Context";
9 import * as Duration from "effect/Duration";
10 import * as Effect from "effect/Effect";
11 +import * as Fiber from "effect/Fiber";
12 import * as Deferred from "effect/Deferred";
13 import * as Path from "effect/Path";
14 import * as PubSub from "effect/PubSub";
@@ -259,7 +260,7 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
260 readonly shell?: boolean;
261 }): Effect.fn.Return<string, never, ChildProcessSpawner.ChildProcessSpawner> {
262 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
262 - const output = yield* Effect.scoped(
263 + const fiber = yield* Effect.scoped(
264 Effect.gen(function* () {
265 const child = yield* spawner.spawn(
266 ChildProcess.make(input.command, input.args, {
@@ -302,7 +303,12 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
303 DesktopShellEnvironmentCommandError: (error) =>
304 logShellEnvironmentCommandError(error).pipe(Effect.as("")),
305 }),
306 + Effect.forkDetach,
307 + );
308 + const output = yield* Fiber.join(fiber).pipe(
309 Effect.timeoutOption(input.timeout),
310 + // Interrupting awaits the child's exit, which a shell stuck in the kernel never reaches.
311 + Effect.ensuring(Fiber.interrupt(fiber).pipe(Effect.forkDetach)),
312 );
313 if (Option.isSome(output)) {
314 return output.value;
packages/shared/src/shell.test.ts
+30 -1
@@ -9,6 +9,7 @@ import * as Fiber from "effect/Fiber";
9 import * as FileSystem from "effect/FileSystem";
10 import * as Path from "effect/Path";
11 import * as Option from "effect/Option";
12 +import * as TestClock from "effect/testing/TestClock";
13 import * as NodeChildProcess from "node:child_process";
14 import * as NodeFS from "node:fs";
15 import * as NodeOS from "node:os";
@@ -42,10 +43,12 @@ const decodeDesktopHostMessage = Schema.decodeUnknownSync(DesktopHostTelemetryMe
43
44 const nativeProbe = vi.hoisted(() => ({
45 execFile: vi.fn<typeof import("node:child_process").execFile>(),
46 + original: undefined as unknown as typeof import("node:child_process").execFile,
47 }));
48
49 vi.mock("node:child_process", async (importOriginal) => {
50 const original = await importOriginal<typeof import("node:child_process")>();
51 + nativeProbe.original = original.execFile;
52 return { ...original, execFile: nativeProbe.execFile.mockImplementation(original.execFile) };
53 });
54
@@ -275,7 +278,7 @@ describe("readEnvironmentFromWindowsShell", () => {
278 Effect.gen(function* () {
279 const started = Promise.withResolvers<void>();
280 const kill = vi.fn(() => true);
278 - const child = { kill } as unknown as ReturnType<
281 + const child = { kill, unref: vi.fn() } as unknown as ReturnType<
282 typeof import("node:child_process").execFile
283 >;
284 const execFile = nativeProbe.execFile;
@@ -297,6 +300,32 @@ describe("readEnvironmentFromWindowsShell", () => {
300 }),
301 );
302
303 + effectIt.effect("gives up on a shell that survives the kill and holds stdout open", () =>
304 + Effect.gen(function* () {
305 + const stdout = { destroy: vi.fn() };
306 + const unref = vi.fn();
307 + const child = { kill: vi.fn(() => true), stdout, unref } as unknown as ReturnType<
308 + typeof import("node:child_process").execFile
309 + >;
310 + const execFile = nativeProbe.execFile;
311 + execFile.mockClear();
312 + execFile.mockImplementation(() => child);
313 + try {
314 + const fiber = yield* readEnvironmentFromWindowsShell(["PATH"]).pipe(Effect.forkChild);
315 + yield* Effect.yieldNow;
316 + yield* TestClock.adjust("6 seconds");
317 + expect(execFile.mock.calls.map(([file]) => file)).toEqual(["pwsh.exe", "powershell.exe"]);
318 + yield* TestClock.adjust("6 seconds");
319 + expect(yield* Fiber.join(fiber)).toEqual({});
320 + expect(stdout.destroy).toHaveBeenCalledTimes(2);
321 + expect(unref).toHaveBeenCalledTimes(2);
322 + } finally {
323 + execFile.mockReset();
324 + execFile.mockImplementation(nativeProbe.original);
325 + }
326 + }),
327 + );
328 +
329 effectIt.effect("extracts environment variables from a PowerShell command", () =>
330 Effect.gen(function* () {
331 const execFile = vi.fn<
packages/shared/src/shell.ts
+11 -1
@@ -5,6 +5,7 @@ import * as NodeChildProcess from "node:child_process";
5 import * as NodeFS from "node:fs";
6 import * as Cache from "effect/Cache";
7 import * as Clock from "effect/Clock";
8 +import * as Duration from "effect/Duration";
9 import * as Effect from "effect/Effect";
10 import * as FileSystem from "effect/FileSystem";
11 import * as Option from "effect/Option";
@@ -17,6 +18,9 @@ const SHELL_ENV_NAME_PATTERN = /^[A-Z0-9_]+$/;
18 const WINDOWS_PATH_DELIMITER = ";";
19 const POSIX_PATH_DELIMITER = ":";
20 const WINDOWS_SHELL_CANDIDATES = ["pwsh.exe", "powershell.exe"] as const;
21 +const WINDOWS_SHELL_PROBE_TIMEOUT_MS = 5000;
22 +// A shell stuck in the kernel survives the kill and holds stdout, so execFile's callback never fires.
23 +const WINDOWS_SHELL_PROBE_DEADLINE = Duration.millis(WINDOWS_SHELL_PROBE_TIMEOUT_MS + 1000);
24
25 type ExecFileSyncLike = (
26 file: string,
@@ -360,6 +364,9 @@ const execWindowsEnvironmentProbe = (
364 }
365 return Effect.sync(() => {
366 child.kill();
367 + child.stdout?.destroy();
368 + child.stderr?.destroy();
369 + child.unref();
370 });
371 });
372
@@ -380,7 +387,10 @@ export const readEnvironmentFromWindowsShell = Effect.fn("shell.readEnvironmentF
387 command,
388 ];
389 for (const shell of WINDOWS_SHELL_CANDIDATES) {
383 - const output = yield* execFile(shell, args, { encoding: "utf8", timeout: 5000 });
390 + const output = yield* execFile(shell, args, {
391 + encoding: "utf8",
392 + timeout: WINDOWS_SHELL_PROBE_TIMEOUT_MS,
393 + }).pipe(Effect.timeoutOption(WINDOWS_SHELL_PROBE_DEADLINE), Effect.map(Option.flatten));
394 if (Option.isNone(output)) continue;
395
396 const environment: Partial<Record<string, string>> = {};