Repository navigation
Enforce the MCP sandbox memory limit - #5464
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.setMemoryLimittherefore 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.filldo 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:WebAssembly.Memorysized 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.memoryLimitBytesbelow 16 MiB or above 2 GiB throws aRangeError, 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.tsandskills/mcp/SKILL.mdnow 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:ok === false, or the phrase "was stopped" that every stop message shares, now assert the reason.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:
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--mcppasses (125 tests).pnpm exec turbo run prepare:bundled --filter=bb-plugin-bb--mcpbuilds, and the bundle imports.Probes through
runSandboxedScript, not committed:ArrayBufferglobal all stop with the memory message within 75 ms, with no unhandled rejection and no teardown failure.Not run: Windows, and a real MCP client against the app.
🤖 Generated with Claude Code