Skip to content

fix(tail): avoid preallocating from the requested line count - #319

Merged
ferdingler merged 1 commit into
strands-agents:mainfrom
ikenyal:fix/tail-large-line-counts
Nov 19, 2026
Merged

ferdingler merged 1 commit into
strands-agents:mainfrom
ikenyal:fix/tail-large-line-counts

Conversation

@ikenyal

@ikenyal ikenyal commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

tail -n N allocated a ring buffer from N before reading any input. Very large counts could abort the host process, and usize::MAX overflowed in debug builds. Start the ring buffer empty and let it grow with the input, retaining the existing last-N-lines behavior.

Regression coverage checks huge counts on stdin, files and empty input, plus zero, one, exact-length and longer-than-input counts. The maximum-count test fails on unmodified main.

Related Issues

Fixes #311

Documentation PR

Not needed; this restores existing behavior.

Type of Change

Bug fix

Testing

  • 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

Validated on macOS against main after #315 with the full Rust check, workspace doctests, and Node-feature Rust unit tests. Both changes also passed direct CLI checks during initial validation. Existing lint warnings remain.

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

Documentation changes are not needed: this restores existing shell behavior.


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

@github-actions github-actions Bot added the bug Something isn't working label Nov 18, 2026
@ikenyal
ikenyal marked this pull request as ready for review November 18, 2026 23:03
@ferdingler

ferdingler commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

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

Approve. The one-line fix resolves #311, and the new regression test fails on main for the right reason. No findings. @ferdingler

  • Repro: echo a | tail -n 99999999999 aborts at ccae0f8 (exit 134, memory allocation of 2400000000000 bytes failed) and prints a with exit 0 at 2372871.
  • Regression test: tail_large_line_counts_preserve_small_input fails on base source with a panic at src/commands/tail.rs:64 and passes at head.
  • Suite: cargo test --workspace --all-targets at head gave 2641 passed, 0 failed, 0 ignored (Linux aarch64, Rust 1.100.0, debug profile).
  • Not verified: doc tests, --features node tests, the Python and Node suites, clippy, and a release build.
  • CI at this SHA: Rust on ubuntu and windows is green. The macOS jobs were still queued or running when I looked, and CI Gate was red at that point.
What I ran

Head 2372871575717d3598d1a01954bbb5a5d283a063, base ccae0f8039fae3bc1d76a901e1a273952f5b89e2. Each tree was built into its own target directory, and the two binaries were run side by side.

Command Base Head
echo a | tail -n 99999999999 abort, exit 134 a
echo a | tail -99999999999 abort, exit 134 a
echo a | tail --lines=99999999999 abort, exit 134 a
tail -n 99999999999 /tmp/f abort, exit 134 a
echo a | tail -n 18446744073709551615 panic attempt to add with overflow a
echo a | tail -n 9223372036854775807 panic capacity overflow a
3000 lines through tail -n 99999999999 | wc -l abort, exit 134 3000

Behaviour that should not change is identical on both binaries: default count (last 10 of 25), -n 3, -n 0, -n +24, -n 25 and -n 26 on 25 lines, -n 2000 on 3000 lines (first line 1001, 2000 lines out), and -n 2999 on a 3000-line file.

Regression test on base. I checked out base and overlaid only the head tests/shell_integration.rs, then ran cargo test --test shell_integration tail_: 11 passed, 1 failed. The failure is tail_large_line_counts_preserve_small_input, with a panic at src/commands/tail.rs:64:66 followed by the stdout assertion at line 4502. At head the same command gives 12 passed. The usize::MAX / 2 case panics with capacity overflow rather than an arithmetic overflow, so the test should not depend on debug overflow checks; I only ran the debug profile.

Other checks. rustfmt --edition 2024 --check is clean on both changed files. src/commands/tail.rs:64 was the only place in tail.rs where the count sized an allocation; the +N path and the legacy -N form do not allocate from it.

Appendix: non-blocking, all true on main as well (3)
  • Count above usize::MAX falls back to 10. src/commands/tail.rs:25 uses val.parse().unwrap_or(10), so 25 lines through tail -n 99999999999999999999999 print 10 on both base and head. Outside #311 and not filed.
  • A panicking pipeline stage reports exit 0. src/exec.rs:4097-4102 leaves the stage's exit at 0 when its task fails. On base, echo a | tail -n 18446744073709551615 panics and the shell exits 0. I found no command that triggers it at head, so not filed.
  • Unrelated to this PR: echo $(( (-9223372036854775807-1) / -1 )) panics at src/exec.rs:1244 (attempt to divide with overflow, exit 101) in a debug build at head. Not checked in release and not filed; I can open an issue if wanted.

@ferdingler
ferdingler merged commit b8aa900 into strands-agents:main Nov 19, 2026
34 of 63 checks passed
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] tail -n with a huge count aborts the process with an allocation failure

3 participants