Repository navigation
fix: reduce stack usage during recursive shell execution - #318
Closed
gautamsirdeshmukh wants to merge 4 commits into
Closed
gautamsirdeshmukh wants to merge 4 commits into
gautamsirdeshmukh wants to merge 4 commits into
Conversation
Contributor
|
Context from the Claude Code case in #323. An embedder runs the shell on a thread with the default 2 MiB stack. There, a plain function recursion #324 removes the recursion that Claude Code's |
Contributor
Author
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.
Description
Finite recursive shell calls can consume a normal 2 MiB worker stack before reaching the configured depth limit. In Box, this leaves too little stack for Cedar and causes policy evaluation to report
recursion limit reachedand deny commands.Finish pipeline preparation and builtin I/O setup before executing nested commands. Their large temporary stack frames then return before recursion starts. Construct the remaining large futures outside recursive poll frames and keep the 8 KiB read buffer on the heap. Executable lookup and redirection helpers own their allocation boundaries. Control-flow futures use the same constructor boundary, so even unused branches do not reserve temporary space in each recursive frame.
Execution keeps the same thread, borrowed process, task locals, permission checks, cancellation behavior, and configured depth limit. There is no production stack-growth dependency;
stackeris a dev dependency for measuring callback headroom. Native stack use still grows with recursion, so arbitrary nesting is not guaranteed.Related Issues
Related to strands-agents/staging-boxy#585. Box's diagnostic buffering and exit presentation are separate work.
Documentation PR
None. This changes internal allocation and preserves the public API and configuration.
Type of Change
Bug fix
Testing
On macOS arm64:
CARGO_INCREMENTAL=0 cargo +1.98.1 xtask check --rust-onlypasses with normal debug settings: formatting, clippy, all 2,642 Rust tests, and documentation with warnings denied. No new lint warnings.An isolated Box build completes function depths 40 and 60,
evalat its last permitted depth, and recursivefind -execandxargsat depth 31. Each run uses 33 custom policies plus generated policies. No recursion warnings.Regression coverage checks 512 KiB of callback headroom, policy denial, captured execution, the configured depth limit, and cancellation.
Nine randomized release benchmark rounds measured 7.4% to 20.4% lower latency than upstream. Depth 60 takes 1.36 ms versus upstream's 1.56 ms; with 100 async resumptions, 3.07 ms versus 3.31 ms. Candidate stacks are 2 MiB; upstream requires 8 MiB for the deep cases. These are local Shell microbenchmarks, not end-to-end Box latency measurements.
The callback-headroom regression also passes on x86_64 macOS under Rosetta with normal debug settings. Linux, Windows, Python, Node.js, and WASM were not exercised locally; CI checks the new revision.
I ran the relevant test suites for the bindings I touched (
cargo test --workspace --all-targets,pytest tests/python,npm test)If I touched Rust, I ran
cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.