Skip to content

fix: stop a command when its substitution uses up the deadline - #257

Merged
ferdingler merged 6 commits into
strands-agents:mainfrom
ferdingler:fix/deadline-after-substitution
Nov 13, 2026
Merged

ferdingler merged 6 commits into
strands-agents:mainfrom
ferdingler:fix/deadline-after-substitution

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When the execution timeout ran out inside a $(...), the command around it still ran. So something like echo $(sleep 5) > out with a short timeout reported a timeout, but out still got created, and the outer command still did its work. For anything with side effects (deleting files, writing output), that means work happens after the deadline already passed.

Now the shell checks the deadline once a command's words and redirect targets are expanded, before it starts the command or opens any redirect. If the time is up, the command is skipped and you get the usual timeout error. Small change in the executor, plus tests.

I added tests that run a slow substitution under a short timeout in a few shapes: in an argument, in a redirect target, in an env prefix, in a pipeline, nested inside another substitution, and an append to an existing file. Each one checks that nothing ran and no file was created or changed. All of them fail on main and pass with the change. A substitution that finishes in time still lets the command run. The full Rust suite passes, and clippy shows no new warnings. One thing I noticed: a timeout inside a nested substitution prints the timeout message once per level. That already happens on main for loops inside a substitution, so I left it alone.

Related Issues

None.

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 command substitution that ran past the execution deadline let the outer command run and open its redirect targets. The pipeline now checks the deadline after it expands words and redirect targets, before it dispatches any stage or applies a redirect.
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 23:12
@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, with two non-blocking fixes. @ferdingler, the fix does what it says: the command and its redirects no longer run once a substitution has used up the deadline. I checked about 70 shell inputs on main and on this branch to confirm it.

  • 🟡 One timeout now prints the message twice when another command follows: echo $(sleep 5); touch f, and also with ||, if/else, until, or a newline. On main it prints once. See the inline comment on src/exec.rs:3183.
  • 🟡 The new test breaks if #256 merges first. With #256, sleep reports strands-shell: sleep: execution timeout exceeded, and the test's exact line match fails. There's a one-line suggestion inline.
  • Question (non-blocking): a here-doc body is expanded later than this check, so cat <<EOF > out with $(sleep 5) in the body still runs cat and writes out. That's the same bug through a different path (exec.rs:3175 → 1643). Fix it here or in a follow-up?

No public API change. All tests pass: 16 targets, 2362 passed, 0 failed.

✅ What I verified
  • Head 5f875e9, base c7c2202. Cargo wasn't in the sandbox, so I installed it with rustup (cargo 1.100.0).
  • cargo test --workspace --all-targets ran all 16 targets, including mcp_integration: 2362 passed, 0 failed. cargo clippy --all-targets gave no clippy lints. The only warnings are the existing snake-case ones (grep_F_fixed and friends) and a manifest homepage warning, none of them in this diff.
  • Adversarial probes: 70+ inputs under a 200 ms timeout, each run with this change and with it reverted, checking status, stdout, stderr, and side effects. The change stops these after the deadline, which main did not: redirect file created, rm, mv, cd, export, assignment-only x=$(…), alias, command, exec, function redirects, an earlier pipeline stage, 2> redirects. errexit, xtrace (no more + echo for the skipped command), and find -exec each now print the message once instead of twice.
  • I applied #256's sleep.rs hunk on top of this branch: timeout_in_substitution_stops_the_command_and_its_redirect fails, with stderr="strands-shell: sleep: execution timeout exceeded\nstrands-shell: execution timeout exceeded\n".
  • I ran the dedupe change from the inline comment against the full shell_integration suite (1778 passed) and all the probes. It removes the duplicate in 8 shapes and changes nothing else.
  • Process note: my reviewer and adversarial subagent passes both timed out at 900s (the sandbox disk filled during their builds). I didn't retry them. I ran the probes myself and covered the reviewer checks (scope against the PR body, the tests, docs, conventions) by hand.

Artifacts, uploaded to s3://strandly-prod-artifacts-319426459652/strands-agents/shell/pr/257/: adv-probes.rs, probe-compare-main-vs-pr.txt, probes-and-shell_integration-pr.log, probes-main.log, dedupe-variant.log, cargo-test-full.log.

Questions

Non-blocking: other paths where a substitution can still use up the deadline and the command runs anyway. These behave the same on main. They're listed so you can decide the scope:

  • Here-doc body: cat <<EOF > /tmp/after\n$(sleep 5)body\nEOF → file created with body. tee /tmp/after <<EOF … behaves the same way.
  • eval, ./source, and compound commands with a substituted redirect target: eval 'echo hi' > "/tmp/$(sleep 5)after", { echo hi; } > "/tmp/$(sleep 5)after", for …; done > …. All of these create the file. eval/source go through run_special_redirected (exec.rs:2701), not run_pipeline.
  • exit 0$(sleep 5) returns status 0 with no timeout message (exec.rs:2909-2918).
  • : ${x:=$(sleep 5)default} still assigns x=default.
Reading order for human reviewers

src/exec.rs:3182-3186, then execute_command_line_inner at exec.rs:2166-2172 and execute_item at exec.rs:2188, which explain the duplicate, then the tests at tests/shell_integration.rs:5081.

Appendix — non-blocking (5)
  • ⚪ set -u; echo $(sleep 5) $nope used to exit 2 (nounset). It now exits 1, with the nounset message followed by the timeout message, because the timeout check runs before the nounset check. That ordering seems right to me; mentioning it only because the status changes.
  • ⚪ ! echo $(sleep 5) still prints the timeout message twice even with the dedupe change: negation flips the status to 0, so the post-item check fires a second time.
  • Pre-existing, filed #260: a substitution that starts after the deadline reports maximum recursion depth exceeded. exec.rs:1685 prints a hardcoded message whatever check_limits() returns. Repro: echo $(sleep 5) $(echo second).
  • Pre-existing, filed #261: exec CMD ARGS expands its arguments twice (exec.rs:2942, then again via 2955), so exec echo $(echo once >> /tmp/count; echo x) writes once twice.
  • Not a gap: f() { echo $(sleep 5); }; f > /tmp/after still creates /tmp/after. That's expected: the redirect opens before the function body runs, and the deadline runs out inside the body.

Comment thread src/exec.rs Resolved
Comment thread tests/shell_integration.rs Resolved
The new deadline check in run_pipeline printed the timeout message and returned status 1. The command list then went on, and the check of the next item printed the message again. Thus `echo $(sleep 5); touch f` printed the message two times, and main prints it one time.

A pipeline that stops at the deadline now sets a flag. The callers of the pipeline read the flag and end the command list with exit 1, as the check after an item does on main. A command substitution gives the flag to the command that holds it, so that command does not print the message again.
… 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.
…bstitution

# Conflicts:
#	src/exec.rs
#	src/os.rs
@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. @ferdingler, both earlier findings are fixed, and I found nothing new worth blocking on.

  • ✅ Duplicate timeout message: fixed in 3e36c5f. I re-ran 31 timeout shapes. The 8 that used to print twice now print once and stop the list, and so do ! …, { :; } | cmd, nested substitutions, function bodies, eval, source, and for/while bodies. Keeping the timeout line when a command fails for an unrelated reason is better than the snippet I suggested.
  • ✅ Test breaks once #256 lands: fixed in fc57f8d. With #256's src/ changes (a52ceb1) applied on top, both substitution tests pass.
  • I checked that the new deadline_stop flag doesn't stick around: a second shell.run after a stop behaves normally in all 31 shapes. The 8 max_depth probes give the same output as main.

Tests: cargo test --workspace --all-targets at 8ad4ab2, 16 targets, 2514 passed, 0 failed. cargo fmt --check is clean. cargo clippy --all-targets reports nothing on lines this PR touches.

Appendix — non-blocking (2)
  • ⚪ echo $(sleep 5) & wait; touch /tmp/after prints the timeout message twice (main prints it once). The background job runs on a forked process, so its deadline_stop never reaches the parent, and wait's limit check prints a second time. Rare, so fine to leave. Subshells, f | cat, and trap … EXIT also print twice, but they did on main too.
  • My earlier question about here-docs is still open: cat <<EOF > out with $(sleep 5) in the body still runs cat. A follow-up is fine.

Artifacts in s3://strandly-prod-artifacts-319426459652/strands-agents/shell/pr/257/: followup-probes.rs, followup-probes-pr.log, followup-probes-main.log, followup-cargo-test-full.log.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent can you re-approve please

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

Approving at a488e22, @ferdingler. The only new commit since my last review is the merge from main, which brings in #256. The PR's own changes against main are identical to what I reviewed at 8ad4ab2 (the same src/exec.rs, src/os.rs, and tests/shell_integration.rs diff). With #256 merged in, cargo test --workspace --all-targets passes locally: 16 targets, 2523 passed, 0 failed. CI is green on this commit too.

@ferdingler
ferdingler merged commit 909465b into strands-agents:main Nov 13, 2026
30 checks passed
@ferdingler
ferdingler deleted the fix/deadline-after-substitution branch November 13, 2026 03:43
ferdingler added a commit to ferdingler/shell that referenced this pull request Nov 13, 2026
Merges strands-agents#257 and strands-agents#264. The deadline stop after a compound piped into a
pipeline now runs after the caller's stdin is given back. Stdin set aside
around a substitution or a pipeline stage keeps the outside-input marker, so
Lua still reads it as empty.
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