Skip to content

Commit 92595ab

Browse files
voltcrashmaria-rcksjuliusmarminge
authored
fix(server): isolate Linux process launches from the RPC event loop (#30209)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent 854b61b commit 92595ab

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

‎apps/server/src/process/SpawnWorker.test.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as NodeOS from "node:os";
33
import * as NodePath from "node:path";
44
import * as NodeServices from "@effect/platform-node/NodeServices";
55
import { describe, expect, it } from "@effect/vitest";
6+
import { vi } from "vite-plus/test";
67
import * as Deferred from "effect/Deferred";
78
import * as Effect from "effect/Effect";
89
import * as Fiber from "effect/Fiber";
@@ -15,19 +16,27 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
1516

1617
import * as SpawnWorker from "./SpawnWorker.ts";
1718

19+
const envChdir = vi.hoisted(() => ({ supported: true }));
20+
vi.mock("@t3tools/shared/envTrampoline", async (importOriginal) => ({
21+
...(await importOriginal<typeof import("@t3tools/shared/envTrampoline")>()),
22+
supportsEnvChdir: () => Promise.resolve(envChdir.supported),
23+
}));
24+
1825
const layer = SpawnWorker.layer.pipe(Layer.provide(NodeServices.layer));
1926
const text = <E>(stream: Stream.Stream<Uint8Array, E>) =>
2027
stream.pipe(Stream.decodeText(), Stream.mkString);
2128
const node = (script: string, args: ReadonlyArray<string> = [], options = {}) =>
2229
ChildProcess.make(process.execPath, ["-e", script, ...args], options);
2330

2431
describe("SpawnWorker", () => {
25-
it.live("runs the server's Windows children on a launcher thread, not the server thread", () =>
32+
it.live("runs children on a launcher thread where the server thread would wait on them", () =>
2633
Effect.gen(function* () {
27-
for (const [platform, onServerThread] of [
28-
["win32", false],
29-
["linux", true],
34+
for (const [platform, hasEnvChdir, onServerThread] of [
35+
["win32", true, false],
36+
["linux", true, true],
37+
["linux", false, false],
3038
] as const) {
39+
envChdir.supported = hasEnvChdir;
3140
const launched = Effect.gen(function* () {
3241
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3342
const child = yield* spawner.spawn(

‎apps/server/src/process/SpawnWorker.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import * as NodeWorkerThreads from "node:worker_threads";
66
import * as NodeChildProcessSpawner from "@effect/platform-node-shared/NodeChildProcessSpawner";
77
import * as Effect from "effect/Effect";
88
import * as Layer from "effect/Layer";
9+
import { supportsEnvChdir } from "@t3tools/shared/envTrampoline";
910
import * as HostProcess from "@t3tools/shared/HostProcess";
1011

1112
// Windows runs CreateProcess synchronously on the calling thread, and antivirus or a busy disk can
12-
// hold it for seconds. Workers own the children, so only the worker waits.
13+
// hold it for seconds; Linux's spawn waits for chdir and the PATH search, which a hung mount
14+
// stalls. Workers own the children, so only the worker waits.
1315
const MAX_WORKERS = 4;
1416
const MAX_IDLE_WORKERS = 1;
1517

@@ -528,9 +530,12 @@ export const layer = Layer.unwrap(
528530
}),
529531
);
530532

531-
/** Only Windows creates processes slowly enough on the calling thread to need workers. */
533+
/** Workers on Windows, and on Linux where `NonBlockingSpawner` has no GNU `env -C` to use. */
532534
export const layerServer = Layer.unwrap(
533-
Effect.map(HostProcess.Platform, (platform) =>
534-
platform === "win32" ? layer : NodeChildProcessSpawner.layer,
535-
),
535+
Effect.gen(function* () {
536+
const platform = yield* HostProcess.Platform;
537+
if (platform === "win32") return layer;
538+
if (platform === "linux" && !(yield* Effect.promise(supportsEnvChdir))) return layer;
539+
return NodeChildProcessSpawner.layer;
540+
}),
536541
);

0 commit comments

Comments
 (0)