Skip to content

fix(xargs): exit 123 when any invocation fails, like GNU - #223

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/xargs-exit-status
Nov 12, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/xargs-exit-status

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

xargs returned the status of its last run only, so a failure in the middle got lost. With files a and b and no nosuch, on main:

$ printf 'a\nnosuch\nb\n' | xargs -n 1 cat; echo $?
a
b
0

GNU xargs exits 123 there. Now xargs follows GNU's rules: 123 if any run failed, and if the command exits 255 it stops and exits 124. With this change the example above prints 123. One difference from GNU: a command that itself exits 126 or 127 stops xargs here, while GNU only stops when the command can't be run, because inside the shell those two cases return the same status.

I compared each case against GNU xargs on Linux and added tests that fail on main and pass with the change. This doesn't touch option parsing or stderr forwarding, and it merges cleanly with #208 and #213.

Related Issues

Part of #207

Documentation PR

N/A

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.

@ferdingler
ferdingler force-pushed the fix/xargs-exit-status branch from d631e85 to 2e453cf Compare November 12, 2026 22:02
@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 22:02
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 22:02
@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 once the timeout status is fixed. I compared the new status rules with GNU xargs 4.9.0 and they match. Two 🟡 inline, nothing blocking:

  • 🟡 A timeout inside xargs now exits 123. Shell::timeout documents status = 1 (src/shell.rs:1015-1017). With timeout = 1, echo 2 | xargs sleep gives 1 on main and 123 on this PR. There's a fix suggestion inline; I tested it and it gives 1 again.
  • 🟡 xargs_status_not_found also passes on main, so no test checks the new stop-on-127. The stronger test inline passes here and fails on main.

CI: the FAILURE rollup comes from run 43316065263, which was cancelled and superseded by rerun 43316066805. That rerun was still in progress and had no failed jobs when I checked. This PR didn't cause it.

@ferdingler

✅ What I verified
  • Head 2e453cf. cargo test --workspace --all-targets: 2207 passed, 0 failed.
  • New tests with main's xargs.rs swapped in: 6 of 7 fail (with the strengthened xargs_status_not_found). Only xargs_status_all_succeed passes, as expected. On the PR head, all 7 pass.
  • Against GNU 4.9.0:
    • A failure in the middle, at the end, or under -I gives 123.
    • 255 gives 124, and the abort message is byte-identical to GNU's.
    • A missing command gives 127 with one error. main printed it twice.
    • All runs succeeding gives 0.
  • set -e, || and if around xargs behave like bash with GNU xargs (exit 123, the else branch sees 123).
  • Callers: tests/differential/cases/xargs_*.sh, tests/command_fallback.rs:274 and tests/command_check.rs:425 still pass. No docs or tool descriptions mention xargs status.
  • Timeout repro, with [limits] timeout = 1: strands-shell --config t.toml -c 'echo 2 | xargs sleep' gives main rc=1, PR rc=123, and PR + suggestion rc=1. The 123 only reaches the final status when xargs is the last command. Every limit check (src/exec.rs:2163-2201) returns 1, so a limit status never collides with stops().
  • Process: an advanced-tier adversarial pass timed out at 900s. I re-ran it, scoped to limits and set -e, and it finished; the timeout finding comes from that run.
Appendix — non-blocking (4)
  • ⚪ 126/127 deviation (documented): run_script also returns 126 when it can't open or read a script (src/exec.rs:160-178). So xargs -n 1 sh stops at a missing script, where GNU carries on and returns 123. The trade-off seems fine to me. A test pinning 0\n127\n7 → stops at 127 would stop a later change from flipping it without anyone noticing.
  • ⚪ Pre-existing, not filed: exit N isn't masked to 8 bits ((exit 256); echo $? prints 256). Practically unreachable.
  • ⚪ Pre-existing: printf '' | xargs false returns 0. GNU, without -r, runs the command once and returns 123.
  • ⚪ With -I X X …, the abort message names the unsubstituted X (cmd_args[0]). Contrived.

Comment thread src/builtins/xargs.rs Resolved
Comment thread tests/shell_integration.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

Both findings are fixed in bd2c189. Timeout results stay at the documented status 1 across normal, -n, and -I paths. The missing-command test now asserts exactly one diagnostic, and I verified it fails if stop-on-127 is removed.

The timeout regression failed before the fix and passes now. Full workspace tests, fmt, clippy, doc tests, and warning-denied docs pass, along with seven GNU comparisons and three CLI timeout cases. The documented 126/127 trade-off and the pre-existing empty-input behavior remain as described.

@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. I checked both findings at bd2c189 and both are fixed. I found nothing new in the fix.

  • ✅ Timeout status: the check_limits() guard is at src/builtins/xargs.rs:147-149. xargs_status_timeout covers the default, -n and -I paths. When I removed the guard, the test failed with [123, 123, 123] against [1, 1, 1].
  • ✅ Stop-on-127: xargs_status_not_found now asserts a single diagnostic. When I removed 127 from stops(), the test failed.

CI on bd2c189 was still pending when I checked: the Windows Node jobs had passed and the rest were queued.

✅ What I verified
  • cargo test --workspace --all-targets at bd2c189: 2208 passed, 0 failed. That's one more than before, the new timeout test.
  • Both mutation checks above were reverted afterwards, and the worktree is clean.
  • Attacking the fix:
    • If the deadline expires during a final run that exits 0, xargs now returns 1. That matches the shell's own check after a successful command (src/exec.rs:2162-2167), so it's consistent rather than a regression.
    • The guard runs on the parent proc, whose depth has already passed check_limits, so only the deadline can trigger it.
    • The 200 ms test deadline isn't flaky. If CI is slow enough for the deadline to hit before xargs runs, execute_item still returns 1 with the same message, and the whole xargs filter ran in 0.64 s.
  • The 126/127 trade-off and the empty-input behaviour are unchanged, as you noted. They're still non-blocking.

@ferdingler
ferdingler merged commit c7c2202 into strands-agents:main Nov 12, 2026
31 checks passed
@ferdingler
ferdingler deleted the fix/xargs-exit-status branch November 12, 2026 23:46
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