Skip to content

Commit 2a6312c

Browse files
authored
fix: two independent hanging-write bugs in the execution engine and local Bun server (#158)
* fix(execution): fork sandbox as daemon so pause/resume survives runPromise boundaries The execution engine's `startPausableExecution` forked the sandbox fiber with `Effect.fork`, which attaches it to the current fiber's `Local` FiberScope. Every host that drives `executeWithPause` and `resume` from separate contexts (the HTTP API handlers, CLI `executor call` + `executor resume`, and any host that pauses across an async boundary) runs each call in its own top-level `Effect.runPromise`. When the first runPromise's root fiber exits to return the paused result, its `FiberRuntime.evaluateEffect` calls `interruptAllChildren()` and interrupts the sandbox before the caller ever sees the response. The subsequent `resume` then races `Fiber.join(paused.fiber)` — which returns the interrupt exit immediately (converted to a defect by `Effect.orDie` in `awaitCompletionOrPause`) — against `Deferred.await(nextSignal)`. `Effect.race`'s `onSelfDone` on failure waits for the loser, but nothing ever signals the next pause Deferred: the tool's continuation runs on a separate root fiber spawned by the quickjs sandbox bridge for each tool call, and once that completes with no further elicitations, `nextSignal` is never filled. The resume call hangs forever. Crucially, the underlying HTTP side effect of the tool can still succeed in this window — producing "phantom writes" where the upstream observes the mutation but the caller never sees the response. `Effect.forkDaemon` attaches the sandbox fiber to the global FiberScope instead of the parent's children set, so the parent's `interruptAllChildren` on exit does not touch it. FiberRefs and Context are copied at fork time via `unsafeMakeChildFiber`, so the daemon is fully independent of the driving runPromise's lifetime. Adds a regression test (`"resume returns across separate runPromise boundaries for a single-elicit tool (HTTP-like)"`) that uses plain `it(async () => ...)` so each engine call is its own top-level `runPromise`, matching the HTTP handler shape. The test runs against a new single-elicit `singleApproval` tool that mirrors the Gmail `labels.create`-style "one approval, one side effect, one response" shape. The existing `multiApproval`-based `it.effect` test masks the bug because both calls share one runPromise scope AND the second elicit eventually fills `nextSignal`, letting the race complete even with a dead sandbox fiber. * fix(local): disable Bun.serve idleTimeout so interactive MCP elicitation round-trips can complete Bun.serve's default idleTimeout is 10 seconds (see `src/bun.js/api/server/ServerConfig.zig:26`). That is too short for the executor's HTTP endpoints: - The MCP streamable-HTTP transport on `/mcp` keeps the TCP socket open for the duration of a tool call. When a non-SAFE operation trips the `requiresApproval` annotation in the google-discovery / openapi / graphql plugins, the executor's elicitation handler calls `server.server.elicitInput(...)`, which sends an `elicitation/create` request to the MCP client and awaits its response. For clients that surface an approval UI to a human operator, the full round-trip — render prompt, read it, click, serialize the response, send it back over the socket — routinely takes 5–10 seconds. Bun's 10s idle timeout was closing the socket mid-flight, either: (a) before the response returned — the tool call appeared to hang and the caller saw a transport error; or (b) after the sandbox had already received `{ action: "accept" }` and the underlying HTTP call to the upstream API had gone through — producing "phantom writes" where the upstream observed the mutation but the caller never saw the response. Both failure modes were reproduced against Gmail's `labels.create` during debugging. - The executor's own `/api/executions` + `/api/executions/:id/resume` pause/resume flow also benefits: long-running sandboxed computations (e.g. large batch operations driven from a `tools.search` loop) can legitimately idle the socket beyond 10 seconds while awaiting tool continuations. `idleTimeout: 0` disables the idle timeout entirely. This is a documented behavior of Bun's uSockets: `us_socket_timeout` in `packages/bun-usockets/src/socket.c:86-92` sets the internal `s->timeout` field to the sentinel `255` — which never fires — when `seconds == 0`. `idleTimeout: 0` is therefore the correct "no timeout" signal, not a "close immediately" bug. The executor's MCP host and HTTP API clients are responsible for enforcing their own per-request timeouts (MCP SDK's `DEFAULT_REQUEST_TIMEOUT_MSEC = 60000`, and any caller-side timeouts). Disabling the Bun-level idle timeout only removes a racing cutoff that was shorter than those logical timeouts and shorter than a realistic human approval cycle. * chore(execution,local): trim verbose narrative comments from fix commits * docs: tighten fix rationale comments
1 parent ec8d886 commit 2a6312c

3 files changed

Lines changed: 77 additions & 5 deletions

File tree

‎apps/local/src/serve.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ export async function startServer(opts: StartServerOptions = {}): Promise<Server
106106
const server = Bun.serve({
107107
port,
108108
hostname: "127.0.0.1",
109+
// Disable Bun's default 10s idle timeout. MCP elicitation and pause/resume
110+
// can idle longer during human approval; `0` disables the socket timeout.
111+
idleTimeout: 0,
109112
routes: { ...staticRoutes },
110113
async fetch(req) {
111114
if (!isAllowedHost(req)) {

‎packages/core/execution/src/engine.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,10 @@ export const createExecutionEngine = (config: ExecutionEngineConfig): ExecutionE
317317
);
318318

319319
/**
320-
* Start an execution in the pause/resume mode. Forks the sandbox
321-
* onto its own fiber and waits for either completion or the first
322-
* elicitation pause.
320+
* Start an execution in pause/resume mode.
321+
*
322+
* The sandbox is forked as a daemon because paused executions can outlive the
323+
* caller scope that returned the first pause, such as an HTTP request handler.
323324
*/
324325
const startPausableExecution = (code: string): Effect.Effect<ExecutionResult> =>
325326
Effect.gen(function* () {
@@ -353,7 +354,7 @@ export const createExecutionEngine = (config: ExecutionEngineConfig): ExecutionE
353354
});
354355

355356
const invoker = makeFullInvoker(executor, { onElicitation: elicitationHandler });
356-
fiber = yield* Effect.fork(codeExecutor.execute(code, invoker));
357+
fiber = yield* Effect.forkDaemon(codeExecutor.execute(code, invoker));
357358

358359
const initialSignal = yield* Ref.get(pauseSignalRef);
359360
return yield* awaitCompletionOrPause(fiber, initialSignal);

‎packages/core/execution/src/tool-invoker.test.ts‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "@effect/vitest";
2-
import { Effect, Schema } from "effect";
2+
import { Effect, Fiber, Schema } from "effect";
33

44
import {
55
ElicitationResponse,
@@ -293,6 +293,22 @@ describe("pause/resume with multiple elicitations", () => {
293293
return { first: r1, second: r2 };
294294
}),
295295
}),
296+
tool({
297+
name: "singleApproval",
298+
description:
299+
"A tool that elicits exactly once and then returns a value. Mirrors the shape of a typical `gmail.users.labels.create` style operation: one approval, one side effect, one success response.",
300+
inputSchema: EmptyInput,
301+
handler: (_args, ctx) =>
302+
Effect.gen(function* () {
303+
const r = yield* ctx.elicit(
304+
new FormElicitation({
305+
message: "Only approval",
306+
requestedSchema: {},
307+
}),
308+
);
309+
return { ok: true, response: r };
310+
}),
311+
}),
296312
],
297313
}),
298314
] as const,
@@ -346,4 +362,56 @@ describe("pause/resume with multiple elicitations", () => {
346362
}),
347363
{ timeout: 10000 },
348364
);
365+
366+
// Regression: use separate top-level runPromise calls to match HTTP/CLI
367+
// pause/resume, and a single-elicit tool so no later pause can mask a dead
368+
// sandbox fiber.
369+
it(
370+
"resume returns across separate runPromise boundaries for a single-elicit tool (HTTP-like)",
371+
async () => {
372+
const executor = await Effect.runPromise(makeElicitingExecutor());
373+
const engine = createExecutionEngine({ executor });
374+
375+
const code = "return await tools.api.singleApproval({});";
376+
377+
const outcome1 = await engine.executeWithPause(code);
378+
expect(outcome1.status).toBe("paused");
379+
const paused1 = outcome1 as Extract<typeof outcome1, { status: "paused" }>;
380+
expect(paused1.execution.elicitationContext.request.message).toBe("Only approval");
381+
382+
// `execution.fiber` is on `InternalPausedExecution`; the exported
383+
// `PausedExecution` type doesn't carry it. Cast to read.
384+
const sandboxFiber = (
385+
paused1.execution as unknown as {
386+
readonly fiber: Fiber.Fiber<unknown, unknown>;
387+
}
388+
).fiber;
389+
const exitProbe = await Effect.runPromise(
390+
Effect.race(
391+
Fiber.await(sandboxFiber),
392+
Effect.map(Effect.sleep("50 millis"), () => "still-running" as const),
393+
),
394+
);
395+
expect(exitProbe).toBe("still-running");
396+
397+
const outcome2 = await Promise.race([
398+
engine.resume(paused1.execution.id, { action: "accept" }),
399+
new Promise<never>((_, reject) =>
400+
setTimeout(
401+
() => reject(new Error("resume hung across runPromise boundaries")),
402+
2000,
403+
),
404+
),
405+
]);
406+
407+
expect(outcome2).not.toBeNull();
408+
const resumed = outcome2 as NonNullable<typeof outcome2>;
409+
expect(resumed.status).toBe("completed");
410+
if (resumed.status === "completed") {
411+
expect(resumed.result.error).toBeUndefined();
412+
expect(resumed.result.result).toMatchObject({ ok: true });
413+
}
414+
},
415+
10000,
416+
);
349417
});

0 commit comments

Comments
 (0)