You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] A function used as a pipeline stage emits nothing once its output passes max_output, and replaces non-UTF-8 bytes #382
Run with the CLI and default limits (max_output is 1 MiB), in an empty directory: strands-shell -c '<script>'.
l=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
l=$l$l$l$l$l$l$l$l
l=$l$l$l$l$l$l$l$l
i=0
while [ $i -lt 100 ]; do echo "$i$l"; i=$((i+1)); done > a
f() { cat; }
cat a a a | f | wc -c
echo "status=$?"
cat a a a | cat | wc -c
cat a | f | wc -c
printf '\377\376\n' | f | base64
printf '\377\376\n' | cat | base64
a is 409,890 bytes, so cat a a a sends 1,229,670 bytes through the stage.
Expected Behavior
A function stage passes its output on as a plain command stage does. bash 5.2.15 prints:
1229670
status=0
1229670
409890
//4K
//4K
Actual Behavior
0
status=0
1229670
409890
77+977+9Cg==
//4K
Over 1 MiB the function stage writes nothing downstream. The status is 0 and stderr is empty, so the caller cannot tell that data was lost. Below the limit the bytes arrive, but \377\376 becomes two U+FFFD characters.
Additional Context
Found while reviewing #357, which had the same symptom for command NAME stages until it stopped routing them through this path.
In src/exec.rs at dbb6189, a function stage runs its body with state_proc.capture = true (line 4278). The body's pipelines then drain into a String with os::read_to_string_lossy_limited(&mut reader, max_output).await.unwrap_or_default() (lines 4371-4377). read_to_string_lossy_limited (src/os.rs:1048) returns Err when the limit is passed, so unwrap_or_default() turns the whole output into an empty string, and it converts bytes that are not UTF-8 lossily. The captured text is written to the stage's stdout only after the body finishes.
I did not test the Python, Node.js or WASM bindings.
Possible Solution
Let the commands in a function stage's body write to the stage's stdout directly, as ordinary stages do, so the output streams and stays as bytes. If the capture has to stay, report the overflow on stderr with a non-zero status.
Activity