Skip to content

[BUG] >> inside a loop takes time quadratic in the output file's size, so appending 4.6 MB line by line hits the 30 s timeout #388

Open

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell (built from main at dbb6189)
  • 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 dbb6189, cargo build --release)

Language Runtime Version

Rust 1.100.0

Operating System

Debian 12 (aarch64)

Installation Method

built from source

Steps to Reproduce

With N set to 6000, 12000, 24000 and 60000, and L a line of 76 a characters:

$ strands-shell -c "i=0; while [ \$i -lt $N ]; do echo $L >> /tmp/o; i=\$((i+1)); done; wc -c < /tmp/o"

Expected Behavior

Time grows linearly with the number of appended lines. bash takes 0.85 s for the 60,000-line case.

Actual Behavior

Release build, default 30 s timeout, one run each:

Lines appended Final file size strands-shell bash 5.2.15
6,000 462,000 bytes 0.48 s 0.09 s
12,000 924,000 bytes 1.59 s 0.18 s
24,000 1,848,000 bytes 5.89 s 0.34 s
60,000 4,620,000 bytes 30.01 s, strands-shell: execution timeout exceeded, exit 1 0.85 s

Time goes up about 3.5 times for each doubling. Control: the same 12,000 lines written through one redirect on the loop (while …; done > /tmp/o) take 0.26 s.

Additional Context

Found while reviewing #378 (the fix for #338). That PR does not change this: its head 3cde1aa gives the same times (0.47 s, 1.62 s, 5.82 s). The two meet in a common script: while read -r l; do echo "$l" >> out; done < in is linear on the read side after #378 and still quadratic on the write side.

Every open with >> reads the whole existing file into a new buffer, adds the new bytes, and writes the whole buffer back when the writer closes. For a VFS file this is src/vfs_kernel.rs:413-430 at 3cde1aa:

let mut buf = if append {
    let v = vfs_ref.lock().await;
    v.read_file(ino).unwrap_or(&[]).to_vec()
} else {
    Vec::new()
};
while let Some(chunk) = rx.recv().await {
    …
    buf.extend_from_slice(&chunk);
}
let mut v = vfs_ref.lock().await;
let _ = v.write_file(ino, buf);

The two direct-bind paths have the same shape (src/vfs_kernel.rs:191-203 and 249-… at dbb6189: tokio::fs::read(&path) / std::fs::read(&path), then a full write). I measured only the VFS path (/tmp/o).

Possible Solution

For an append open, keep only the new bytes and add them to the end of the file when the writer closes: an append method on the VFS for in-memory files, and OpenOptions::append(true) for direct binds. The max_file_size check then needs the current file length rather than the buffer length.

Related Issues

#338, #378

Activity

  1. added
    bug
    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