Skip to content

[BUG] while read …; done < file takes time quadratic in the file size, so a 2 MB file hits the 30 s timeout #338

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell (built from main at 2a1495e)
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Binding

Rust

Strands Shell Version

0.0.0 (built from source, main 2a1495e, cargo build --release)

Language Runtime Version

Rust 1.100.0

Operating System

Debian 12 (aarch64)

Installation Method

built from source

Steps to Reproduce

With KB set to 190, 375, 750 and 1500 (base64 makes the file about 1.37 times larger, in 77-byte lines):

$ strands-shell -c "head -c $((KB*1024)) /dev/urandom | base64 > /tmp/f; wc -c < /tmp/f; n=0; while read -r l; do n=\$((n+1)); done < /tmp/f; echo lines_read=\$n"

Expected Behavior

Time grows linearly with the file size. bash reads the 2.07 MB file in 0.18 s.

Actual Behavior

Release build, default 30 s timeout:

File size Lines Wall time Result
262,830 bytes 3,414 0.73 s ok
518,737 bytes 6,737 2.76 s ok
1,037,474 bytes 13,474 11.17 s ok
2,074,948 bytes 26,948 30.00 s strands-shell: execution timeout exceeded, exit 1

Time goes up about 4 times for each doubling. Controls on the 2.07 MB file: wc -l < /tmp/f; grep -c . /tmp/f takes 0.02 s, and the same loop fed through a pipe (cat /tmp/f | while read -r l; …) takes 0.58 s. One run each.

Additional Context

Found while testing #326; that PR does not change this. read takes one byte per call (src/builtins/read.rs, "Read byte-by-byte to avoid BufReader consuming extra data"). A redirected file reaches the reader as one large chunk, the unread part is kept in a Vec<u8>, and every one-byte read removes its byte from the front of that Vec, which moves all the remaining bytes (src/os.rs:1150-1154 at 2a1495e):

if !remainder.is_empty() {
    let n = remainder.len().min(buf.remaining());
    buf.put_slice(&remainder[..n]);
    remainder.drain(..n);
    return Poll::Ready(Ok(()));
}

A pipe delivers small chunks, which is why the cat … | form is fast.

Possible Solution

Keep the unread part so that taking bytes from the front is cheap: a bytes::Bytes (split_to/advance), a VecDeque<u8>, or a Vec with a read offset.

Related Issues

#326

Activity

  1. added
    bug
    on Nov 20, 2026
  2. added a commit that references this issue on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Fields

    Language

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions