Repository navigation
Commit 2a6312c
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 comments1 parent ec8d886 commit 2a6312c
3 files changed
Lines changed: 77 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
109 | 112 | | |
110 | 113 | | |
111 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
317 | 317 | | |
318 | 318 | | |
319 | 319 | | |
320 | | - | |
321 | | - | |
322 | | - | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
323 | 324 | | |
324 | 325 | | |
325 | 326 | | |
| |||
353 | 354 | | |
354 | 355 | | |
355 | 356 | | |
356 | | - | |
| 357 | + | |
357 | 358 | | |
358 | 359 | | |
359 | 360 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
293 | 293 | | |
294 | 294 | | |
295 | 295 | | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
296 | 312 | | |
297 | 313 | | |
298 | 314 | | |
| |||
346 | 362 | | |
347 | 363 | | |
348 | 364 | | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
349 | 417 | | |
0 commit comments