Skip to content

fix(sleep): report a failure when the time limit cuts a sleep short - #256

Merged
ferdingler merged 8 commits into
strands-agents:mainfrom
ferdingler:fix/sleep-cut-short-status
Nov 13, 2026
Merged

ferdingler merged 8 commits into
strands-agents:mainfrom
ferdingler:fix/sleep-cut-short-status

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Description

A native sleep cut short by the time limit still reported success to kernels that record each command's status. Reporting its failure also exposed paths that could run more commands or change files after the deadline.

Interrupted native sleeps now fail, and execution stops before later commands or file opens can use incomplete substitutions in arguments or redirect filenames. Heredoc timeouts, timeout-message deduplication, and integration with #257's overlapping dispatch guard remain follow-ups.

I reproduced the data loss before the guards; with them, the full Rust checks and 62 CLI/MCP probes pass. CI also passes for the Python and Node bindings across Linux, macOS, and Windows. The CLI eval/source positives include a short settling wait because the prior revision can exit with direct-bind writes still pending. I didn't run Python, Node, or WASM locally; the bindings and WASM sleep path are unchanged.

Related Issues

Found in an audit of Box's move from its own copy of Shell to upstream (strands-agents/box#414). Box's old copy had this fix.

Documentation PR

None.

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • 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.

A sleep that the execution deadline cut short returned status 0. The
shell still failed the run, but a kernel that records each command's
status saw the sleep as a success.

The deadline branch now fails the command with the timeout error, the
same way a host command cut short by the deadline fails. The stage
status becomes 1 and stderr names the command.
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 23:10
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent please review

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

Verdict: changes requested. The sleep fix itself is correct, but it lets || and else fallbacks run after the deadline inside $(...) and in piped { … } | cmd groups. A 4-line fix is below and passes the full suite.

  • 🟡 Fallbacks run after the timeout inside $(). x=$(sleep 5 || echo ran > /out/marker) with timeout = 1 now writes the file to the host. On main it doesn't. Details are inline on sleep.rs:37.
  • 🟡 Shell::timeout doc no longer matches. src/shell.rs:1017 promises stderr contains `strands-shell: execution timeout exceeded` . A lone sleep 5 now prints strands-shell: sleep: execution timeout exceeded, so a caller who matches on the documented string misses the most common timeout. Suggested wording: an `execution timeout exceeded` message in stderr.

@ferdingler, the first one is your call: fix it here (my preference, since this PR is what makes it reachable) or in a follow-up.

✅ What I verified · how I reviewed · appendix

Verified at head 2cdd3f8. That's your merge of main. The PR's diff is the same as at 653e992, which is where I started.

  • cargo test --workspace --all-targets: 2439 passed, 0 failed. At 653e992: 2361 passed.
  • The new a_sleep_cut_short_by_the_deadline_reports_a_failure test fails when the base sleep.rs is swapped in (it records Finished(..., 0)), so it really tests the fix.
  • Top-level control flow is fine: &&, ||, !, if, while, set -e, pipelines, ( ), { }, functions, & wait, traps, Lua os.execute/io.popen, MCP. Nothing runs after the deadline. ! sleep 5 now prints a timeout message where main printed none, which is an improvement.
  • No Python, Node, README or MCP text pins the old message.

Repro (CLI --config with [limits] timeout = 1 and a mode = "direct" bind at /out):

command main 83685f4 PR 2cdd3f8 PR + fix
x=$(sleep 5 || echo ran > /out/marker) no file file written no file
x=$(if sleep 5; then :; else echo ran > /out/marker; fi) no file file written no file
{ sleep 5 || echo x > /out/marker; } | cat no file file written no file
sleep 5 || echo ran > /out/marker (top level) no file no file no file

With the fix, cargo test --workspace --all-targets still passes: 2439 passed, 0 failed. The artifacts are under strands-agents/shell/pr/256/ in the evidence bucket: repro-cmdsubst-fallback.sh, repro-cmdsubst-fallback-output.txt, suggested-fix-run_capturing_status.diff, cargo-test-head.log and cargo-test-with-fix.log.

How I reviewed. One correctness/control-flow pass ran, because this is a small logic change. I skipped the API bar-raiser pass because no public type, function or parameter changed; the doc mismatch above is the only surface effect. I skipped a separate adversarial pass because the correctness pass already worked through the control-flow matrix.

Reading order: src/commands/sleep.rs, then src/exec.rs (run_capturing_status against execute_command_line_inner), then the tests.

Appendix: non-blocking (2)

  • ⚪ This bug predates the PR: $(sleep 5; echo after) already runs after past the deadline on main. The suggested fix closes it too, so I haven't filed a separate issue.
  • ⚪ wasm32 is unchanged by this PR. sleep there still blocks for the full duration (sleep.rs:24-28) and never reports the cut short, so its stderr will differ from native builds. I didn't build wasm.

Comment thread src/commands/sleep.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

I also updated the timeout API documentation in 4b2ef7f to describe the diagnostic without requiring the shell-only prefix, so it covers a lone interrupted sleep. The guard closes the appendix's sequential-command case too.

I left the WASM sleep implementation unchanged and did not build WASM locally. The extra generic timeout line in some captured forms remains a diagnostic-deduplication follow-up; it does not allow more commands to run.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

ferdingler added a commit to ferdingler/shell that referenced this pull request Nov 13, 2026
… substitution test

A sleep that the deadline stops will report `strands-shell: sleep: execution timeout exceeded` when strands-agents#256 merges. The test now accepts each line that ends with the shared timeout text.

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

Verdict: changes requested. Both earlier findings are fixed, but the fix I suggested opened a worse hole: when the deadline cuts a substitution short, the command it feeds still runs, now with a truncated argument. On the host, rm -rf "/out/$(sleep 5; echo sub)" now deletes all of /out. On main it deletes only /out/sub. A 5-line fix is inline and passes the full suite.

Previous findings

  • ✅ Fallbacks running after the deadline inside $(): fixed. Both new tests fail when the exec.rs guard is reverted, so they really cover it.
  • ✅ Shell::timeout doc: fixed.
  • 🔴 New, from the guard plus the sleep change: a substitution that hits the deadline can now come back empty or cut short, and the outer command runs anyway. Details are inline.

Sorry, @ferdingler: this one is on my earlier suggestion.

✅ What I verified · repro · appendix

Head 4b2ef7f. cargo test --workspace --all-targets: 2514 passed, 0 failed. With the inline run_pipeline fix applied: still 2514 passed, 0 failed.

Repro. CLI --config with [limits] timeout = 1 and [[bind]] mode = "direct" mounted at /out. Before each run, /out holds sub/ and keep/important. "PR without guard" is this head with only the exec.rs guard reverted.

command main 119d352 PR without guard PR 4b2ef7f PR + inline fix
rm -rf "/out/$(sleep 5; echo sub)" removes sub removes sub deletes the bind dir nothing removed
rm -rf "/out/$(sleep 5 && echo sub)" removes sub deletes the bind dir deletes the bind dir nothing removed
rm -rf "/out/$(sleep 5)" deletes the bind dir deletes the bind dir deletes the bind dir nothing removed
d=$(sleep 5; echo sub); rm -rf "/out/$d" nothing removed nothing removed nothing removed nothing removed

The last row is safe because execute_item checks the deadline before the rm. The third row shows the root cause already exists on main; this PR extends it to the ; and && forms. Commands that run within the time limit (x=$(sleep 0.1; echo ok)) are unchanged.

I also checked the guard against:

  • { … } > file, { … } 2>&1 and while loops inside $(): correct.
  • Recursion depth: the check is consistent with the one in execute_item.
  • Within-limit || and && inside $(): unchanged.

Artifacts are under strands-agents/shell/pr/256/ in the evidence bucket: repro-subst-timeout-rm.txt, suggested-fix-run_pipeline.diff, cargo-test-4b2ef7f.log and cargo-test-4b2ef7f-with-run_pipeline-fix.log.

Appendix: non-blocking (2)

  • ⚪ The timeout line prints twice in some captured forms. The inline fix adds one more case, d=$(...); rm, which prints it three times. This is cosmetic, and I agree with leaving it to a follow-up.
  • ⚪ wasm sleep is unchanged by this PR and I didn't build it, same as last round.

Comment thread src/exec.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent please review

@ferdingler

Copy link
Copy Markdown
Collaborator Author

I followed the timed-out substitution through the remaining redirect paths. Even with the outer-command guard in a52ceb1, { echo new; } > "/out/$(printf keep; sleep 5; printf suffix)" could truncate keep, and a captured <> redirect could create a file after the deadline. Captured exit, eval, and source had the same gap.

3989421 checks the limits after those expansions and before opening redirects or running the special command. The new direct-bind regressions reproduce truncation and creation without the checks, then preserve the files with them. They cover groups, subshells, captured control flow, and direct/captured eval and source. The full Rust tests, doctests, formatting, clippy, and warning-denied docs pass, along with 62 CLI/MCP probes including normal redirects within the deadline.

I also tested the combined patch with #257 locally: the changes merge cleanly and the timeout safety tests pass. Its dispatch guard overlaps with this one; when integrating them, the added guards should share its deadline_stop reporting flag to avoid duplicate generic timeout messages. That diagnostic cleanup remains separate. Python, Node, and WASM were not run locally; their code is unchanged.

Validation note: standalone within-limit CLI eval/source redirects sometimes return success without finishing their direct-bind writes. I reproduced this on a52ceb1 before these additions; the same MCP cases pass. The two positive probes append sleep 0.05 to let writes settle before CLI shutdown. Timeout probes are unchanged, and this existing shutdown behavior remains separate.

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

Verdict: approve, with one 🟡 for you to fix here or in a follow-up. a52ceb1 and 3989421 fix the rm -rf data loss: none of the four forms from last round removes anything. The full suite passes at 3989421 (2518 passed, 0 failed). One gap is left: a here-doc body that hits the deadline still overwrites its target file with cut-off content. It's inline, with a fix I ran against this head.

Previous findings

  • ✅ Fallbacks running after the deadline in $(): fixed in 4b2ef7f.
  • ✅ Shell::timeout doc: fixed in 4b2ef7f.
  • ✅ A substitution cut short still fed its command, so rm -rf deleted the bind dir: fixed in a52ceb1. 3989421 also closes it for group redirects, <>, and captured exit/eval/source. The new tests fail when the guards are reverted.
  • 🟡 New: a here-doc body cut short by the deadline truncates the file it's redirected into. Details are inline.

On your 01:28 note: I agree the deadline_stop dedup with #257 and the CLI shutdown write race should stay separate.

✅ What I verified · probes · appendix

Head 3989421. cargo test --workspace --all-targets: 2518 passed, 0 failed. With the here-doc fix applied: 2518 passed, 0 failed, and 14 clippy warnings, the same count as without it.

Probes. CLI --config with [limits] timeout = 1 and a mode = "direct" bind at /out. Before each run, /out holds sub/ and keep/important containing orig.

command main 119d352 PR 3989421 PR + here-doc fix
cat > /out/keep/important <<EOF / key=$(sleep 5; echo val) key=val key= orig (untouched)
{ cat; } > /out/keep/important <<EOF (same body) k=v empty orig
rm -rf "/out/$(sleep 5; echo sub)", …&&…, "/out/$(sleep 5)", d=$(…); rm /out/sub removed, or the bind dir deleted untouched untouched
exec rm, command rm, echo "x$(sleep 5)" | tee, { echo hi; } > "/out/x$(sleep 5)" dir deleted, or a file written untouched untouched
eval, sh -c, case, for, [ -z "$(sleep 5)" ] && rm untouched untouched untouched

I also checked two things against the fix. Recursion depth: every depth += 1 path still goes through execute_item, so the new checks don't trip on their own. Here-doc ordering: cat > f <<EOF with $(cat f) reads the old contents on main and still does with the fix. That already differed from bash, which reads them as empty.

Artifacts are under strands-agents/shell/pr/256/ in the evidence bucket: repro-heredoc-truncation.txt, suggested-fix-heredoc-preexpand.diff, cargo-test-3989421.log, cargo-test-3989421-with-heredoc-fix.log and repro-a52ceb1-probes.txt.

Appendix: non-blocking (3)

  • ⚪ The timeout line still prints two or three times in some forms. You're tracking it with #257's deadline_stop.
  • ⚪ wasm sleep is unchanged by this PR and I didn't build it.
  • ⚪ This predates the PR: capture_output (src/exec.rs:1732) reports maximum recursion depth exceeded for any limit, including an expired deadline. You can see it with command rm "$(sleep 5)". Using the msg that check_limits() returns would fix it. I didn't file it because it's a wrong message, not a wrong action. Your CLI shutdown write race probably deserves its own issue.

Comment thread src/exec.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent please review

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

Verdict: approve. All findings are fixed, and I found nothing new at 5d9cda7.

  • ✅ Here-doc truncation: fixed. No partial body is written in any form I tried. a_here_doc_cut_short_by_the_deadline_writes_no_partial_body fails when the apply_redirects check is reverted.
  • ✅ Your shape is the right one, and my suggestion was wrong. On the in-memory VFS, the hoisted expansion I proposed turns bash's [] into [old]; I had only tested on a direct bind. Last round I also said the ordering was unchanged, which was wrong. Thanks for catching both.
  • Full suite: 2520 passed, 0 failed. Clippy shows 14 warnings, the same count as before.
Probes at 5d9cda7

CLI --config with [limits] timeout = 1 and a mode = "direct" bind at /out. Before each run, /out/keep/important contains orig.

command (body k=$(sleep 5; echo v)) main 119d352 PR 5d9cda7
cat > /out/keep/important <<EOF key=val empty, which matches bash when a timeout kills it
cat <<EOF > /out/keep/important key=val orig
cat >> /out/keep/important <<EOF orig key=val orig
cat <<EOF | tee /out/keep/important k=v orig; tee never runs
while read l; do echo "$l" > /out/…; done <<EOF orig orig
x=$(cat <<EOF …); echo "$x" > /out/… orig orig
quoted <<'EOF' with $(sleep 5) literal text literal text, unchanged
within-limit body $(sleep 0.1; echo fine) ok=fine ok=fine

On the in-memory VFS, echo old > /tmp/f; cat > /tmp/f <<EOF with body [$(cat /tmp/f)] prints [] on main, on 5d9cda7 and in bash. With my hoisted patch it printed [old].

Artifacts are under strands-agents/shell/pr/256/ in the evidence bucket: repro-5d9cda7-heredoc-probes.txt and cargo-test-5d9cda7.log.

Still open from earlier, non-blocking: the duplicate timeout lines (you're handling them with #257 deadline_stop), the capture_output recursion-depth message, and wasm sleep.

@ferdingler
ferdingler merged commit df315f2 into strands-agents:main Nov 13, 2026
30 checks passed
@ferdingler
ferdingler deleted the fix/sleep-cut-short-status branch November 13, 2026 01:59
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.

2 participants