Skip to content

Enforce the MCP sandbox memory limit - #5464

Merged
SawyerHood merged 1 commit into
mainfrom
fix-mcp-sandbox-memory-limit-test
Oct 14, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
fix-mcp-sandbox-memory-limit-test

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

plugins/mcp/src/sandbox.test.ts › "fails cleanly when the script exhausts its memory limit" has timed out at Vitest's 5 s default since #5450 (runs 38469661984, 38472496938, 38485481595). The sandbox's memory limit was never enforced.

The Emscripten build of QuickJS cannot measure allocation sizes. runtime.dumpMemoryUsage() prints "malloc_usable_size unavailable" and "8.0 per block", and 20 MiB of live data is counted as 13,208 bytes. setMemoryLimit therefore only refuses a single allocation larger than the limit. With a 4 MiB limit the test script kept 1,935 arrays of 100,000 elements alive and grew the WASM heap from 16 MiB to its 2 GiB ceiling before a real out-of-memory error ended it.

The 500 ms deadline did not stop it either. QuickJS consults the interrupt handler on the first poll and then once per 10,000 polls, and native calls such as Array.prototype.fill do not poll, so the handler was consulted once in those 1,935 iterations.

The test only asserted ok === false. Where it passed, it passed because the late finish was reported as the computation limit.

What changed

plugins/mcp/src/sandbox.ts:

  • Each sandbox instance gets its own WebAssembly.Memory sized to the limit (initial equals maximum), so the engine cannot allocate past it. An instance is reused after a clean run and discarded after exhaustion or any error.
  • Running out of memory stops the script with "The script ran out of memory and was stopped. …", including when the script catches the error.
  • Strings copied into the sandbox are checked for room first. The QuickJS glue does not check its own allocation: a host result that did not fit was written through a null pointer, trapped inside the settle callback, escaped as an unhandled rejection, and left the script waiting for the 25 s deadline.
  • Settling a host call runs under a fresh deadline, and a failure there stops the script instead of escaping.
  • memoryLimitBytes below 16 MiB or above 2 GiB throws a RangeError, because the WASM module cannot enforce it. The memory test uses 16 MiB instead of 4.

Behavior change: a script could previously use up to 2 GiB. The 64 MB default is now real, and about 5 MiB of it is interpreter overhead.

The tool description in mcp-handler.ts and skills/mcp/SKILL.md now mention the 64 MB limit.

Not changed: the computation deadline still overshoots for loops that spend their time in native calls on large values. Measured overruns ranged from seconds to over a minute. Enforcing it needs the interpreter on a worker thread that can be terminated, which is a separate change.

How you verified

Tests in sandbox.test.ts:

  • The memory test asserts the memory message.
  • New tests: the amount a script can keep scales with its limit (16 MiB and 32 MiB), a script that swallows the error is still stopped, a host result that does not fit stops the script, an out-of-range limit is refused, an unserialisable host result stops the script.
  • Six tests that only checked ok === false, or the phrase "was stopped" that every stop message shares, now assert the reason.
  • Against the old sandbox.ts, six of these fail: the old memory script ends on the computation message, and an unserialisable host result waits 25 s with an unhandled rejection.

Duration of the memory test:

Machine Before After
Local, Node 24 1.80–1.91 s 15–19 ms
Local, Node 22 2.82 s 19 ms
20% CPU quota (about 5.8× slower) timed out at 5 s (ran 12.3 s) 0.30–0.40 s
10% CPU quota (about 12× slower) timed out (ran 28.2 s) 0.40–0.70 s

At the 10% quota all 30 tests in the file pass and the slowest takes 2.5 s.

Commands:

  • pnpm exec turbo run test typecheck lint --filter=bb-plugin-bb--mcp passes (125 tests).
  • pnpm exec turbo run prepare:bundled --filter=bb-plugin-bb--mcp builds, and the bundle imports.

Probes through runSandboxedScript, not committed:

  • Scripts that fill the heap completely, let a pending host call settle into a full heap, receive an oversized host result or error, or replace the ArrayBuffer global all stop with the memory message within 75 ms, with no unhandled rejection and no teardown failure.
  • 200 exhausted runs keep resident memory flat.
  • 300 sequential trivial runs take a median of 0.40 ms each, the same as before.

Not run: Windows, and a real MCP client against the app.

🤖 Generated with Claude Code

AGENT GENERATED

The sandbox test "fails cleanly when the script exhausts its memory limit"
timed out in CI because the limit was never enforced. The Emscripten build
of QuickJS reports "malloc_usable_size unavailable" and counts 8 bytes per
allocation, so setMemoryLimit only rejects a single allocation larger than
the limit. With a 4 MiB limit the test script kept 1,935 arrays of 100,000
elements alive and grew the WASM heap from 16 MiB to its 2 GiB ceiling
before a real out-of-memory error ended it. The interrupt handler is
consulted once per 10,000 interpreter polls and native calls such as
Array.prototype.fill do not poll, so the 500 ms deadline was never checked
during those 1,935 iterations. The run took 1.8 to 1.9 s here and over 5 s
on CI, and passed only because the late finish was reported as the
computation limit.

Each sandbox instance now gets its own WebAssembly memory sized to the
limit, so the engine cannot allocate past it. Running out of memory stops
the script with its own message, and the instance is discarded instead of
reused. Strings copied into the sandbox are checked for room first, because
the QuickJS glue writes through a null pointer when its allocation fails: a
host result that did not fit trapped inside the settle callback and escaped
as an unhandled rejection. Settling a host call now runs under a fresh
deadline, and a failure there stops the script instead of escaping.

Limits below 16 MiB or above 2 GiB are refused, since the WASM module
cannot enforce them.

The memory test asserts the memory message, and new tests cover the limit
scaling with its setting, a script that swallows the error, a host result
that does not fit, and an unserialisable host result. Tests that only
checked ok === false now assert the reason.

Co-Authored-By: Claude Code <[email protected]>
@SawyerHood
SawyerHood marked this pull request as ready for review October 14, 2026 01:12
@SawyerHood
SawyerHood merged commit a7d5dfc into main Oct 14, 2026
28 checks passed
@SawyerHood
SawyerHood deleted the fix-mcp-sandbox-memory-limit-test branch October 14, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant