Skip to content
5 changes: 5 additions & 0 deletions .changeset/proxy-process-blocked-event-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform-node-shared": patch
---

Fix `layerProxyProcess` stopping a healthy proxy process when a blocked event loop delays reading a queued reply past `responseTimeout`.
8 changes: 4 additions & 4 deletions packages/platform/node-shared/src/NodeChildProcessSpawner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1006,10 +1006,10 @@ export class ProxyProcess extends Context.Service<ProxyProcess, {
* `PlatformError`. Its command was not sent, so retrying is safe. After the
* layer's scope closes, spawns fail without starting a proxy process.
*
* A spawn request is sent at most once. If the proxy process exits, or sends
* nothing within `responseTimeout`, after a request was sent but before it
* replied, the command may already be running. The proxy process is stopped,
* the spawn fails with a `PlatformError`, and the request is not sent again.
* A spawn request is sent at most once. If the proxy process exits or does not
* reply within `responseTimeout` after a request is sent, the command may
* already be running. The proxy process is stopped, the spawn fails with a
* `PlatformError`, and the request is not sent again.
*
* **Gotchas**
*
Expand Down
11 changes: 8 additions & 3 deletions packages/platform/node-shared/src/internal/proxyProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,14 @@ class Proxy implements ProxiedProcessOwner {
private startTimer(key: number | "ready"): void {
const millis = this.options.responseTimeoutMillis
const timer = setTimeout(() => {
this.stop(
Object.assign(new Error(`The proxy process did not respond within ${millis} ms`), { code: "ETIMEDOUT" })
)
// Defer past the I/O poll so queued replies are read first; keep
// referenced so an idle loop still runs the check immediately.
setImmediate(() => {
if (this.timers.get(key) !== timer) return
this.stop(
Object.assign(new Error(`The proxy process did not respond within ${millis} ms`), { code: "ETIMEDOUT" })
)
})
}, millis)
// Whatever waits for the reply keeps the application running
timer.unref()
Expand Down
47 changes: 43 additions & 4 deletions packages/platform/node-shared/test/NodeChildProcessSpawner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,29 @@ ${proxyProcessSource}`
while (isAlive(pid)) yield* Effect.sleep("10 millis")
}).pipe(Effect.provide(NodeFileSystem.layer), Effect.scoped))

it.live("keeps a proxy process whose reply waits behind a blocked event loop", () =>
Effect.gen(function*() {
const started = join(yield* FileSystem.FileSystem.use((fs) => fs.makeTempDirectoryScoped()), "started")
yield* Effect.gen(function*() {
const first = Option.getOrThrow(yield* currentPid)
const spawned = yield* Effect.forkChild(
ChildProcessSpawner.use((spawner) =>
spawner.string(ChildProcess.make("sh", ["-c", "> \"$1\" && echo $PPID", "_", started]))
),
{ startImmediately: true }
)
const wait = new Int32Array(new SharedArrayBuffer(4))
const start = performance.now()
while (performance.now() - start < 5_000 && !NodeFs.existsSync(started)) {
Atomics.wait(wait, 0, 0, 10)
}
Atomics.wait(wait, 0, 0, Math.max(1_100 - (performance.now() - start), 20))
assert.isTrue(NodeFs.existsSync(started))
assert.strictEqual(Number((yield* Fiber.join(spawned)).trim()), first)
assert.strictEqual(yield* proxyPid, first)
}).pipe(Effect.provide(proxyServices({ responseTimeout: "1 second" })))
}).pipe(Effect.provide(NodeFileSystem.layer), Effect.scoped))

it("generates the default launcher's program from the worker that run calls", () => {
const script = join(__dirname, "..", "scripts", "generateProxyProcessSource.ts")
const result = spawnSync(process.execPath, [script, "--check"], { encoding: "utf8" })
Expand Down Expand Up @@ -230,8 +253,8 @@ ${proxyProcessSource}`
const runs = join(directory, "runs")
const dropFlag = join(directory, "drop")
yield* fs.writeFileString(dropFlag, "")
// The first proxy process starts commands but never acknowledges them
const program = `const dropSpawned = (() => {
// The first proxy process starts the first command but never acknowledges it
const program = `let drop = (() => {
try {
process.getBuiltinModule("node:fs").rmSync(${JSON.stringify(dropFlag)})
return true
Expand All @@ -240,12 +263,28 @@ ${proxyProcessSource}`
}
})()
const sendMessage = process.send.bind(process)
process.send = (message, ...rest) => dropSpawned && message._tag === "Spawned" ? true : sendMessage(message, ...rest)
process.send = (message, ...rest) => {
if (drop && message._tag === "Spawned") {
drop = false
return true
}
return sendMessage(message, ...rest)
}
${proxyProcessSource}`

yield* Effect.gen(function*() {
const first = Option.getOrThrow(yield* currentPid)
const error = yield* Effect.flip(Effect.scoped(ChildProcess.make("sh", ["-c", "echo run >> \"$0\"", runs])))
let replies = 0
const error = yield* Effect.scoped(ChildProcess.make("sh", ["-c", "echo run >> \"$0\"", runs])).pipe(
Effect.flip,
// Unrelated IPC on the same proxy must not extend a missing reply's deadline
Effect.raceFirst(
Effect.forever(Effect.delay(proxyPid, "100 millis").pipe(Effect.tap(() => Effect.sync(() => replies++))))
.pipe(Effect.ignore, Effect.andThen(Effect.never))
),
Effect.timeout("4 seconds")
)
assert.isAbove(replies, 0)
assert.include(String((error.reason.cause as Error).message), "may have started")
assert.deepStrictEqual(yield* currentPid, Option.none())
// The command ran once
Expand Down
Loading