Repository navigation
fix: stop a command when its substitution uses up the deadline - #257
Conversation
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.
|
@strandly-the-agent please review |
strandly-the-agent
left a comment
There was a problem hiding this comment.
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 onsrc/exec.rs:3183. - 🟡 The new test breaks if #256 merges first. With #256,
sleepreportsstrands-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 > outwith$(sleep 5)in the body still runscatand writesout. 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, basec7c2202. Cargo wasn't in the sandbox, so I installed it with rustup (cargo 1.100.0). cargo test --workspace --all-targetsran all 16 targets, includingmcp_integration: 2362 passed, 0 failed.cargo clippy --all-targetsgave no clippy lints. The only warnings are the existing snake-case ones (grep_F_fixedand friends) and a manifesthomepagewarning, 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-onlyx=$(…), alias,command,exec, function redirects, an earlier pipeline stage,2>redirects.errexit,xtrace(no more+ echofor the skipped command), andfind -execeach now print the message once instead of twice. - I applied #256's
sleep.rshunk on top of this branch:timeout_in_substitution_stops_the_command_and_its_redirectfails, 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_integrationsuite (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 withbody.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/sourcego throughrun_special_redirected(exec.rs:2701), notrun_pipeline.exit 0$(sleep 5)returns status 0 with no timeout message (exec.rs:2909-2918).: ${x:=$(sleep 5)default}still assignsx=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) $nopeused 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:1685prints a hardcoded message whatevercheck_limits()returns. Repro:echo $(sleep 5) $(echo second). - Pre-existing, filed #261:
exec CMD ARGSexpands its arguments twice (exec.rs:2942, then again via2955), soexec echo $(echo once >> /tmp/count; echo x)writesoncetwice. - Not a gap:
f() { echo $(sleep 5); }; f > /tmp/afterstill creates/tmp/after. That's expected: the redirect opens before the function body runs, and the deadline runs out inside the body.
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
|
@strandly-the-agent please review |
strandly-the-agent
left a comment
There was a problem hiding this comment.
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, andfor/whilebodies. 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_stopflag doesn't stick around: a secondshell.runafter a stop behaves normally in all 31 shapes. The 8max_depthprobes 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/afterprints the timeout message twice (main prints it once). The background job runs on a forked process, so itsdeadline_stopnever reaches the parent, andwait's limit check prints a second time. Rare, so fine to leave. Subshells,f | cat, andtrap … EXITalso print twice, but they did on main too. - My earlier question about here-docs is still open:
cat <<EOF > outwith$(sleep 5)in the body still runscat. 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.
|
@strandly-the-agent can you re-approve please |
strandly-the-agent
left a comment
There was a problem hiding this comment.
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.
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.
Description
When the execution timeout ran out inside a
$(...), the command around it still ran. So something likeecho $(sleep 5) > outwith a short timeout reported a timeout, butoutstill 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.
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.