Skip to content

fix: give the CLI an 8 MiB stack on every platform - #411

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/406-cli-stack-size
Nov 30, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/406-cli-stack-size

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

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.

  • 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 fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

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
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@github-actions github-actions Bot added the bug Something isn't working label Nov 30, 2026

@strandly-the-agent strandly-the-agent left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_main detaches the GIL before calling run.
  • 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.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks. I'm leaving the unused Clone bound so the public signature stays exactly the same. The Python and Node library APIs are out of scope here; I'll open a follow-up if anyone hits it there.

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 -s 10240.
  • Panic path: same as base. Exit 101, message printed once (tried /dev/full, a closed pipe, fd exhaustion). Through Python it's a PanicException both ways.
  • Exit codes: 32 invocations byte-identical to base, both native and through the Python console script. Covers -c, stdin scripts, --mcp EOF, list-commands, clap errors, --help, --version, a bad --config, and invalid UTF-8.
  • Signals while a command runs (sleep 30 in the REPL and under -c): INT, TERM, HUP and QUIT behave the same as base. trap is virtual and src/ 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_SIZE mutated 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_unwind path has no test, and no Python test calls cli_main.
  • With ulimit -u 1, --version and list-commands now panic because the thread can't be spawned. That's contrived, so I'm not raising it further.

Comment thread src/cli.rs Resolved
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.
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR.

@strandly-the-agent strandly-the-agent left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Clone bound 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 -INT at the prompt exits 0, and Ctrl-Z then fg leaves the tty raw.
  • Exit codes: -c 'exit 3' returns 3, and a stdin script ending in exit 4 returns 4.
  • Tests: cargo test --test shell_integration passed 2076 with 0 failed. That's one fewer than before because the Linux-only test was removed.
  • Clippy: cargo clippy --workspace --all-targets reports 13 warnings on both main and 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 cancelled 2 Approvers for Bots gate, 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.

@ferdingler
ferdingler merged commit 5f94b05 into strands-agents:main Nov 30, 2026
33 of 35 checks passed
@ferdingler
ferdingler deleted the fix/406-cli-stack-size branch November 30, 2026 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] compound_pipe_into_function_redirected_from_cli overflows the stack on Windows CI

2 participants