Skip to content

Commit 686121d

Browse files
fix(server): recover service PATH after a slow login shell probe (pingdotgg#23356)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent b264e11 commit 686121d

2 files changed

Lines changed: 89 additions & 4 deletions

File tree

‎apps/server/src/os-jank.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,59 @@ effectIt.effect("hydrates PATH when the desktop handoff is absent", () =>
6464
),
6565
);
6666

67+
const timedOut = (file: string) =>
68+
Object.assign(new Error(`spawnSync ${file} ETIMEDOUT`), { code: "ETIMEDOUT" });
69+
70+
effectIt.effect("waits longer for a login shell that times out at startup", () =>
71+
Effect.acquireUseRelease(
72+
Effect.sync(() =>
73+
vi.spyOn(NodeChildProcess, "execFileSync").mockImplementation((file, _args, options) => {
74+
// This profile takes 8 seconds while the machine is under load.
75+
if ((options?.timeout ?? 0) < 8_000) throw timedOut(String(file));
76+
return "__T3CODE_ENV_PATH_START__\n/opt/tools/bin\n__T3CODE_ENV_PATH_END__\n";
77+
}),
78+
),
79+
() =>
80+
Effect.gen(function* () {
81+
const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
82+
83+
yield* fixPath().pipe(
84+
Effect.provideService(HostProcess.Platform, "linux"),
85+
Effect.provideService(HostProcess.Environment, env),
86+
Effect.provide(NodeServices.layer),
87+
);
88+
89+
assert.equal(env.PATH, "/opt/tools/bin:/usr/bin");
90+
}),
91+
(probe) => Effect.sync(() => probe.mockRestore()),
92+
),
93+
);
94+
95+
effectIt.effect("waits longer for only one hung login shell", () =>
96+
Effect.acquireUseRelease(
97+
Effect.sync(() =>
98+
vi.spyOn(NodeChildProcess, "execFileSync").mockImplementation((file) => {
99+
throw timedOut(String(file));
100+
}),
101+
),
102+
(probe) =>
103+
Effect.gen(function* () {
104+
const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
105+
106+
yield* fixPath().pipe(
107+
Effect.provideService(HostProcess.Platform, "linux"),
108+
Effect.provideService(HostProcess.Environment, env),
109+
Effect.provide(NodeServices.layer),
110+
);
111+
112+
const timeouts = probe.mock.calls.map(([, , options]) => options?.timeout);
113+
assert.equal(timeouts.filter((timeout) => (timeout ?? 0) > 5_000).length, 1);
114+
assert.equal(env.PATH, "/usr/bin");
115+
}),
116+
(probe) => Effect.sync(() => probe.mockRestore()),
117+
),
118+
);
119+
67120
effectIt.effect("keeps mise's PATH record with the login shell PATH", () =>
68121
Effect.acquireUseRelease(
69122
Effect.sync(() =>

‎apps/server/src/os-jank.ts‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
import * as Effect from "effect/Effect";
1111
import * as FileSystem from "effect/FileSystem";
1212
import * as Path from "effect/Path";
13+
// @effect-diagnostics-next-line nodeBuiltinImport:off -- the shared login shell probe runs synchronously through execFileSync.
14+
import * as NodeChildProcess from "node:child_process";
1315
import * as NodeOS from "node:os";
1416

1517
function logPathHydrationWarning(message: string, error?: unknown): void {
@@ -18,16 +20,46 @@ function logPathHydrationWarning(message: string, error?: unknown): void {
1820
);
1921
}
2022

23+
// mise's record of the PATH entries it added. It must travel with the
24+
// shell's PATH, or `mise x` can rebuild PATH behind a same-named wrapper
25+
// (`exec mise x claude -- claude`) that then re-runs itself forever.
26+
const LOGIN_SHELL_ENVIRONMENT_NAMES = ["PATH", "__MISE_DIFF", "JAVA_HOME"];
27+
const SLOW_LOGIN_SHELL_TIMEOUT_MS = 20_000;
28+
29+
function readSlowLoginShellEnvironment(shell: string): Partial<Record<string, string>> {
30+
return readEnvironmentFromLoginShell(
31+
shell,
32+
LOGIN_SHELL_ENVIRONMENT_NAMES,
33+
(file, args, options) =>
34+
NodeChildProcess.execFileSync(file, args, {
35+
...options,
36+
timeout: SLOW_LOGIN_SHELL_TIMEOUT_MS,
37+
}),
38+
);
39+
}
40+
2141
function hydratePosixPath(env: NodeJS.ProcessEnv, platform: NodeJS.Platform): void {
2242
let shellEnvironment: Partial<Record<string, string>> = {};
43+
let waitedForSlowShell = false;
2344
for (const shell of listLoginShellCandidates(platform, env.SHELL)) {
2445
try {
25-
// mise's record of the PATH entries it added. It must travel with the
26-
// shell's PATH, or `mise x` can rebuild PATH behind a same-named wrapper
27-
// (`exec mise x claude -- claude`) that then re-runs itself forever.
28-
shellEnvironment = readEnvironmentFromLoginShell(shell, ["PATH", "__MISE_DIFF", "JAVA_HOME"]);
46+
shellEnvironment = readEnvironmentFromLoginShell(shell, LOGIN_SHELL_ENVIRONMENT_NAMES);
2947
} catch (error) {
3048
logPathHydrationWarning(`Failed to read PATH from login shell ${shell}.`, error);
49+
// A profile can be slow only while the machine boots. Wait for it once,
50+
// here: providers copy this env when they are built, so a later read
51+
// would never reach them.
52+
if (!waitedForSlowShell && (error as NodeJS.ErrnoException | null)?.code === "ETIMEDOUT") {
53+
waitedForSlowShell = true;
54+
try {
55+
shellEnvironment = readSlowLoginShellEnvironment(shell);
56+
} catch (retryError) {
57+
logPathHydrationWarning(
58+
`Failed to read PATH from slow login shell ${shell}.`,
59+
retryError,
60+
);
61+
}
62+
}
3163
}
3264

3365
if (shellEnvironment.PATH) break;

0 commit comments

Comments
 (0)