The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 11:00 UTC
fix(server): Windows spawn workers no longer leak a short child's output pipe to a sibling (#37180)
Julius Marminge committed
Nov 15, 2026 at 04:33 UTC
2becf31b640695216df4156f48239ddf35a7983c
2 files changed
+64
-4
apps/server/src/process/SpawnWorker.test.ts
+32
@@ -138,6 +138,38 @@ describe("SpawnWorker", () => {
138
}).pipe(Effect.scoped, Effect.provide(layer)),
139
);
140
141
+ it.live("a short child's output ends while a sibling launched beside it still runs", () =>
142
+ Effect.gen(function* () {
143
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
144
+ const untilStdin =
145
+ "process.stdout.write('ready');process.stdin.once('data',()=>process.exit(0))";
146
+ // Workers that own a child stay alive, as beside a running agent, so each round's two
147
+ // launches land on two warm workers at once.
148
+ yield* Effect.all(
149
+ Array.from({ length: 4 }, () =>
150
+ Effect.gen(function* () {
151
+ const holder = yield* spawner.spawn(node(untilStdin));
152
+ expect(yield* holder.stdout.pipe(Stream.take(1), text)).toBe("ready");
153
+ }),
154
+ ),
155
+ { concurrency: "unbounded" },
156
+ );
157
+ for (let round = 0; round < 20; round++) {
158
+ const [sibling, output] = yield* Effect.all(
159
+ [
160
+ spawner.spawn(node(untilStdin, [], { stdin: { stream: Stream.never } })),
161
+ spawner
162
+ .spawn(node("process.stdout.write('ok')"))
163
+ .pipe(Effect.flatMap((child) => text(child.stdout))),
164
+ ],
165
+ { concurrency: "unbounded" },
166
+ );
167
+ expect(output).toBe("ok");
168
+ expect(yield* sibling.isRunning).toBe(true);
169
+ }
170
+ }).pipe(Effect.scoped, Effect.provide(layer)),
171
+ );
172
+
173
it.live("kills running commands, detached or not, and on scope interruption", () =>
174
Effect.gen(function* () {
175
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
apps/server/src/process/SpawnWorker.ts
+32
-4
@@ -60,10 +60,27 @@ type FromWorker =
60
}
61
| { readonly type: "close"; readonly id: number };
62
63
+// Windows makes a child's stdio pipes inheritable for the length of its CreateProcess, so a child
64
+// another worker launches meanwhile inherits them and holds the first child's output open. Every
65
+// pool's workers take this lock around spawn(); it holds the owning worker's threadId.
66
+const windowsSpawnLock = new Int32Array(new SharedArrayBuffer(4));
67
+
68
// Inline source runs from bundles, asar archives and single-executable builds without module files.
69
const workerSource = `
65
-const { parentPort } = require("node:worker_threads");
70
+const { parentPort, threadId, workerData } = require("node:worker_threads");
71
const { spawn } = require("node:child_process");
72
+const { lock } = workerData;
73
+const locked = (launch) => {
74
+ if (lock === undefined) return launch();
75
+ // Wait on the owner compareExchange saw: an unlock after it changes the value, so no wakeup is lost.
76
+ for (let owner; (owner = Atomics.compareExchange(lock, 0, 0, threadId)) !== 0; ) Atomics.wait(lock, 0, owner);
77
+ try {
78
+ return launch();
79
+ } finally {
80
+ Atomics.store(lock, 0, 0);
81
+ Atomics.notify(lock, 0, 1);
82
+ }
83
+};
84
const children = new Map();
85
const post = (message, transfer) => parentPort.postMessage(message, transfer);
86
const serialize = (error) => {
@@ -78,7 +95,7 @@ parentPort.on("message", (request) => {
95
if (request.type === "spawn") {
96
let child;
97
try {
81
- child = spawn(request.command, request.args, request.options);
98
+ child = locked(() => spawn(request.command, request.args, request.options));
99
} catch (error) {
100
post({ type: "error", id, error: serialize(error) });
101
return;
@@ -290,13 +307,19 @@ class SpawnWorker {
307
readonly worker: NodeWorkerThreads.Worker;
308
readonly children = new Map<number, WorkerChild>();
309
readonly pool: SpawnWorkerPool;
310
+ /** Node sets `worker.threadId` to -1 once the thread exits, so keep the id the lock holds. */
311
+ readonly threadId: number;
312
/** Launches sent but not yet answered; the worker is inside CreateProcess for one of them. */
313
launching = 0;
314
closed = false;
315
316
constructor(pool: SpawnWorkerPool) {
317
this.pool = pool;
299
- this.worker = new NodeWorkerThreads.Worker(workerSource, { eval: true });
318
+ this.worker = new NodeWorkerThreads.Worker(workerSource, {
319
+ eval: true,
320
+ workerData: { lock: pool.platform === "win32" ? windowsSpawnLock : undefined },
321
+ });
322
+ this.threadId = this.worker.threadId;
323
this.worker.on("message", (message: FromWorker) => this.onMessage(message));
324
this.worker.on("error", (error) => this.fail(error));
325
this.worker.on("exit", () => this.fail(new Error("The process launcher thread exited.")));
@@ -426,7 +449,12 @@ class SpawnWorker {
449
if (this.closed) return;
450
this.closed = true;
451
this.pool.workers.delete(this);
429
- void this.worker.terminate();
452
+ void this.worker.terminate().then(() => {
453
+ // The thread may have died holding the lock, or between its unlock and notify, or after
454
+ // a notify woke it: free the lock and wake every waiter so none sleeps on a free lock.
455
+ Atomics.compareExchange(windowsSpawnLock, 0, this.threadId, 0);
456
+ Atomics.notify(windowsSpawnLock, 0);
457
+ });
458
}
459
}
460