Repository navigation
fix: give the CLI an 8 MiB stack on every platform - #411
Conversation
The main thread's stack is 1 MiB on Windows, which a function call inside a command substitution already exceeds in a debug build. Run the CLI on a spawned thread with the 8 MiB stack Linux gives its main thread. Fixes strands-agents#406
|
@strandly-the-agent Please review this PR |
There was a problem hiding this comment.
Approve. Superseded: a deeper pass found a REPL signal-handling regression on Linux. See my follow-up review: #411 (review)
What I verified here still holds:
- shell_integration: 2077 tests pass.
- On a 512 KB stack, base overflows and head passes.
- Exit codes survive the thread hop.
cli_maindetaches the GIL before callingrun.- The CLI path uses only the current-thread runtime.
- The CLI module isn't compiled for wasm.
Non-blocking: the unused T: Clone bound, and the Python/Node library APIs still run on the caller's stack.
|
Thanks. I'm leaving the unused |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changing my verdict to changes-requested. A deeper pass found a regression I missed in my earlier approval: on Linux, the interactive prompt no longer reacts to terminal signals (inline). The fix for #406 still works.
Earlier findings: the unused T: Clone bound and the Python/Node library-stack note are unchanged and still non-blocking. Everything I verified before still holds.
What this pass verified (head f0e6205, base 4f838f9, the PR's parent)
- An independent reviewer and an adversarial tester ran on Linux aarch64 with default
ulimit -s10240. - Panic path: same as base. Exit 101, message printed once (tried
/dev/full, a closed pipe, fd exhaustion). Through Python it's aPanicExceptionboth ways. - Exit codes: 32 invocations byte-identical to base, both native and through the Python console script. Covers
-c, stdin scripts,--mcpEOF,list-commands, clap errors,--help,--version, a bad--config, and invalid UTF-8. - Signals while a command runs (
sleep 30in the REPL and under-c): INT, TERM, HUP and QUIT behave the same as base.trapis virtual andsrc/installs no OS signal handlers. - Repeated
cli_main(maturin debug build, CPython 3.13): same as base. Covers 300 sequential calls with the thread count staying at 1, 8 concurrent callers, and a panic followed by another call. - Clippy: 18 warnings on base and 18 on head, so the PR adds none.
- Tests on head: shell_integration 2077, mcp_integration 41, claude_code_preamble 19 and command_registry 5, all passing.
Non-blocking question. STACK_SIZE is now fixed, so on Unix ulimit -s no longer has any effect. With ulimit -s at 10240, this debug script prints reached 16 on base and overflows on head:
n=0; f() { n=$((n+1)); if [ $n -lt 16 ]; then f; else echo reached $n; fi; }; f
Release builds hit the same thing at about 50 nested calls. Is that intended? Spawning only on Windows avoids it. So does a larger reservation: Linux only commits stack pages as they're touched.
Appendix (non-blocking)
- The new test doesn't pin the stack size. With
STACK_SIZEmutated to 1 MiB it still passes in debug, and on base it passes in a release build. It guards debug CI only, and it's Linux-only. - The
join→resume_unwindpath has no test, and no Python test callscli_main. - With
ulimit -u 1,--versionandlist-commandsnow panic because the thread can't be spawned. That's contrived, so I'm not raising it further.
Running `cli::run` on a spawned thread breaks interactive signals on Linux: rustyline needs SIGINT/SIGWINCH/SIGTSTP delivered to its tty `read()` via EINTR, but the kernel sends process-directed signals to the main thread parked in `join()`, not the worker. On Unix the main thread already has a large stack (typically 8 MiB via `ulimit -s`), so the thread is unnecessary. Spawn only on Windows where the default is 1 MiB (strands-agents#406). This also restores `ulimit -s` control over recursion depth on Unix. Remove the Linux-only `cli_runs_deep_scripts_with_a_small_main_thread_stack` test — it relied on `ulimit -s 512` shrinking the main thread and the CLI surviving via its spawned worker, which no longer applies on Unix. The Windows CI job exercises the spawned-thread path.
|
@strandly-the-agent Please review this PR. |
There was a problem hiding this comment.
Approve. 1a3028b fixes the REPL signal regression. The 8 MiB thread is now only spawned on Windows, so on Unix the CLI runs exactly as it does on main.
Earlier findings:
- REPL signals: fixed and verified. I replied on the thread; I don't have permission to resolve it.
- Fixed stack ignoring
ulimit -s: fixed by the same change. - Unused
Clonebound and the Python/Node APIs: left as is by the author's choice. That's fine.
What I verified (head 1a3028b)
- Signals: ran the pty harness on a Linux debug build. Resize redraws the line,
kill -INTat the prompt exits 0, and Ctrl-Z thenfgleaves the tty raw. - Exit codes:
-c 'exit 3'returns 3, and a stdin script ending inexit 4returns 4. - Tests:
cargo test --test shell_integrationpassed 2076 with 0 failed. That's one fewer than before because the Linux-only test was removed. - Clippy:
cargo clippy --workspace --all-targetsreports 13 warnings on bothmainand head. - CI:
Rust (windows-latest)is green on this head, and it runs the #406 test through the CLI. The rollup's only failure is the cancelled2 Approvers for Botsgate, which isn't a code check. - Not run: I don't have Windows, so the spawned-thread path is covered only by CI.
Nit (non-blocking): the comment left at tests/shell_integration.rs:3179 explains a test that no longer exists. The neighbouring #406 test already covers this, so the comment can go.
Description
On Windows the main thread only gets a 1 MB stack, and a fairly ordinary script (a function call plus a command substitution) needs just about that much in a debug build. So the Windows Rust job overflows the stack in one of the CLI tests on every run, which is blocking every open PR. Real Windows users of the CLI could hit the same crash.
Now the CLI does its work on its own thread with an 8 MB stack, the same as Linux gives its main thread, so every platform has the same room. I did it in the shared CLI entry point rather than only in the binary, so the CLI that ships in the Python wheel gets it too. The Python and Node library APIs run shell code on the caller's thread and aren't covered here; I left them out to keep this small. I also added a Linux test that runs the CLI with its main-thread stack shrunk below 1 MB.
Related Issues
Fixes #406
Documentation PR
None
Type of Change
Bug fix
Testing
I reproduced it on Linux by shrinking the stack: on main the script overflows at 900 KB, and with the change it passes even at 512 KB. The new test fails on main and passes with the fix. I don't have a Windows machine, so the Windows CI job here is the real check.
cargo test --workspace --all-targets,pytest tests/python,npm test)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.