Skip to content

Commit 4946281

Browse files
fix(server): wait on inherited bootstrap pipes without fs reads
AI-assisted; verified with the tests and tools listed above.
1 parent c691ee4 commit 4946281

2 files changed

Lines changed: 55 additions & 19 deletions

File tree

‎apps/server/src/bootstrap.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @effect-diagnostics nodeBuiltinImport:off
2+
import * as NodeChildProcess from "node:child_process";
23
import * as NodeFS from "node:fs";
34
import * as NodeStream from "node:stream";
45
import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -233,3 +234,33 @@ it.layer(NodeServices.layer)("readBootstrapEnvelope", (it) => {
233234
}),
234235
);
235236
});
237+
238+
// Real inherited pipe: an fs read would hold a threadpool thread and keep the process alive.
239+
it.live.skipIf(HostProcess.Platform.defaultValue() === "win32")(
240+
"times out and lets the process exit while the bootstrap pipe stays open",
241+
() =>
242+
Effect.promise(async () => {
243+
const child = NodeChildProcess.spawn(
244+
process.execPath,
245+
[
246+
"--input-type=module",
247+
"-e",
248+
`import * as Effect from "effect/Effect";
249+
import * as Schema from "effect/Schema";
250+
import { readBootstrapEnvelope } from "./src/bootstrap.ts";
251+
const exit = await Effect.runPromiseExit(
252+
readBootstrapEnvelope(Schema.String, 3, { timeoutMs: 50 }).pipe(Effect.flip),
253+
);
254+
console.log(exit._tag === "Success" ? exit.value._tag : "unexpected success");`,
255+
],
256+
{ cwd: `${import.meta.dirname}/..`, stdio: ["ignore", "pipe", "inherit", "pipe"] },
257+
);
258+
let stdout = "";
259+
child.stdout?.on("data", (chunk) => (stdout += chunk));
260+
const code = await new Promise((resolve) => child.once("exit", resolve));
261+
child.stdio[3]?.destroy();
262+
263+
assert.equal(stdout.trim(), "BootstrapEnvelopeTimeoutError");
264+
assert.equal(code, 0);
265+
}),
266+
);

‎apps/server/src/bootstrap.ts‎

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ export const readBootstrapEnvelope = Effect.fn("readBootstrapEnvelope")(function
9797
timeoutMs?: number;
9898
},
9999
): Effect.fn.Return<A, BootstrapError> {
100-
yield* Effect.try({
100+
const stat = yield* Effect.try({
101101
try: () => NodeFS.fstatSync(fd),
102102
catch: (cause) => new BootstrapFdStatError({ fd, cause }),
103103
});
104104

105-
const stream = yield* makeBootstrapInputStream(fd);
105+
const stream = yield* makeBootstrapInputStream(fd, stat.isSocket() || stat.isFIFO());
106106

107107
// Covers slow cold starts while staying well under the desktop readiness timeout.
108108
const timeoutMs = options?.timeoutMs ?? 30_000;
@@ -175,11 +175,17 @@ export const readBootstrapEnvelope = Effect.fn("readBootstrapEnvelope")(function
175175

176176
const makeBootstrapInputStream = Effect.fn("bootstrap.makeBootstrapInputStream")(function* (
177177
fd: number,
178+
isPipe: boolean,
178179
) {
179180
const platform = yield* HostProcess.Platform;
180-
const fdPath = resolveFdPath(fd, platform);
181+
const fdPath = isPipe ? undefined : resolveFdPath(fd, platform);
181182
return yield* Effect.try<NodeStream.Readable, BootstrapInputStreamOpenError>({
182183
try: () => {
184+
// fs reads of an empty non-blocking pipe fail with EAGAIN, and blocking ones pin a
185+
// threadpool thread past the timeout; a libuv stream waits for data and destroys cleanly.
186+
if (isPipe) {
187+
return makeSocketBootstrapStream(fd);
188+
}
183189
if (fdPath === undefined) {
184190
return makeDirectBootstrapStream(fd);
185191
}
@@ -212,22 +218,21 @@ const makeBootstrapInputStream = Effect.fn("bootstrap.makeBootstrapInputStream")
212218
});
213219
});
214220

215-
const makeDirectBootstrapStream = (fd: number): NodeStream.Readable => {
216-
try {
217-
return NodeFS.createReadStream("", {
218-
fd,
219-
encoding: "utf8",
220-
autoClose: true,
221-
});
222-
} catch {
223-
const stream = new NodeNet.Socket({
224-
fd,
225-
readable: true,
226-
writable: false,
227-
});
228-
stream.setEncoding("utf8");
229-
return stream;
230-
}
221+
const makeDirectBootstrapStream = (fd: number): NodeStream.Readable =>
222+
NodeFS.createReadStream("", {
223+
fd,
224+
encoding: "utf8",
225+
autoClose: true,
226+
});
227+
228+
const makeSocketBootstrapStream = (fd: number): NodeStream.Readable => {
229+
const stream = new NodeNet.Socket({
230+
fd,
231+
readable: true,
232+
writable: false,
233+
});
234+
stream.setEncoding("utf8");
235+
return stream;
231236
};
232237

233238
// WSL-inherited stdin pipes cannot be reopened via /proc/self/fd/0 (EACCES),

0 commit comments

Comments
 (0)