Skip to content

Commit db87127

Browse files
authored
fix(desktop): stop freezing the main process during the login shell env probe (#63208)
1 parent 01e577f commit db87127

2 files changed

Lines changed: 57 additions & 11 deletions

File tree

‎packages/desktop/src/main/service/shell-env.test.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { describe, expect, test } from "bun:test"
2+
import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
3+
import { tmpdir } from "node:os"
4+
import path from "node:path"
25
import { NodePath } from "@effect/platform-node"
36
import { Effect } from "effect"
47

5-
import { isNushell, parseShellEnv, resolveUserShell } from "./shell-env"
8+
import { isNushell, loadShellEnv, parseShellEnv, resolveUserShell } from "./shell-env"
69

710
describe("shell env", () => {
811
test("parseShellEnv reads null-delimited pairs and ignores invalid entries", () => {
@@ -27,4 +30,28 @@ describe("shell env", () => {
2730
expect(check("C:\\Program Files\\nu.exe")).toBe(true)
2831
expect(check("/bin/zsh")).toBe(false)
2932
})
33+
34+
// The fake shell fails `-il` and holds `-l` until the test releases it, which a blocking probe never lets happen.
35+
test.skipIf(process.platform === "win32")(
36+
"loadShellEnv falls back to -l without blocking the event loop",
37+
async () => {
38+
const root = mkdtempSync(path.join(tmpdir(), "opencode-shell-env-"))
39+
const shell = path.join(root, "shell")
40+
const release = path.join(root, "release")
41+
42+
try {
43+
writeFileSync(
44+
shell,
45+
`#!/bin/sh\n[ "$1" = "-il" ] && exit 1\nwhile [ ! -e "${release}" ]; do sleep 0.01; done\nFAKE_SHELL_ENV=1 exec /bin/sh -c "$3"\n`,
46+
)
47+
chmodSync(shell, 0o755)
48+
const env = Effect.runPromise(loadShellEnv(shell).pipe(Effect.provide(NodePath.layer)))
49+
50+
writeFileSync(release, "")
51+
expect((await env)?.FAKE_SHELL_ENV).toBe("1")
52+
} finally {
53+
rmSync(root, { recursive: true, force: true })
54+
}
55+
},
56+
)
3057
})

‎packages/desktop/src/main/service/shell-env.ts‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { spawnSync } from "node:child_process"
1+
import { spawn } from "node:child_process"
22
import { userInfo } from "node:os"
33
import { Effect, Path } from "effect"
44

55
const TIMEOUT = 5_000
66

77
type Probe = { type: "Loaded"; value: Record<string, string> } | { type: "Timeout" } | { type: "Unavailable" }
88

9+
type Run =
10+
| { type: "Exited"; status: number | null; stdout: Buffer }
11+
| { type: "Error"; error: Error }
12+
| { type: "Timeout" }
13+
914
export function resolveUserShell(envShell: string | undefined, loginShell: string | null | undefined) {
1015
const resolvedLoginShell = loginShell && loginShell !== "unknown" ? loginShell : undefined
1116

@@ -34,18 +39,32 @@ export function parseShellEnv(out: Buffer) {
3439
return env
3540
}
3641

42+
// Async so the main thread keeps painting the window and serving renderer assets while a slow rc file runs.
43+
const run = (shell: string, mode: "-il" | "-l") =>
44+
Effect.callback<Run>((resume) => {
45+
const child = spawn(shell, [mode, "-c", "env -0"], {
46+
stdio: ["ignore", "pipe", "ignore"],
47+
windowsHide: true,
48+
})
49+
50+
const chunks: Buffer[] = []
51+
52+
child.stdout.on("data", (chunk: Buffer) => chunks.push(chunk))
53+
child.on("error", (error) => resume(Effect.succeed({ type: "Error", error })))
54+
child.on("close", (status) => resume(Effect.succeed({ type: "Exited", status, stdout: Buffer.concat(chunks) })))
55+
56+
return Effect.sync(() => {
57+
if (child.exitCode === null && child.signalCode === null) child.kill()
58+
})
59+
}).pipe(Effect.timeoutOrElse({ duration: TIMEOUT, orElse: () => Effect.succeed<Run>({ type: "Timeout" }) }))
60+
3761
const probe = Effect.fn("ShellEnv.probe")(function* (shell: string, mode: "-il" | "-l") {
38-
const out = spawnSync(shell, [mode, "-c", "env -0"], {
39-
stdio: ["ignore", "pipe", "ignore"],
40-
timeout: TIMEOUT,
41-
windowsHide: true,
42-
})
62+
const out = yield* run(shell, mode)
4363

44-
const err = out.error as NodeJS.ErrnoException | undefined
64+
if (out.type === "Timeout") return { type: "Timeout" } satisfies Probe
4565

46-
if (err) {
47-
if (err.code === "ETIMEDOUT") return { type: "Timeout" } satisfies Probe
48-
yield* Effect.logWarning(`[server] Shell env probe failed for ${shell} ${mode}: ${err.message}`)
66+
if (out.type === "Error") {
67+
yield* Effect.logWarning(`[server] Shell env probe failed for ${shell} ${mode}: ${out.error.message}`)
4968

5069
return { type: "Unavailable" } satisfies Probe
5170
}

0 commit comments

Comments
 (0)