Skip to content

[P1] function call-site redirect + external command: redirected stdin is lost after the child spawn (read returns EOF) #647

Closed

Description

@caomengxuan666

Summary

When a shell function is invoked with a call-site redirect and the body runs an external command, the redirected stdin is lost at the first child spawn: the next read returns EOF immediately, so the function processes only the first line. GNU bash 5.3 keeps reading the redirected file. Reproduced in both sh and bash core modes.

Minimal reproduction

printf 'l1\nl2\nl3\nl4\n' > f

F () { while read l; do echo "$l"; /usr/bin/true; done; }
F < f
  • rubash / Niubash 1.4.0: prints l1 only
  • GNU bash 5.3: prints l1, l2, l3, l4

Direct look at the stream after the spawn:

F () {
  while read l; do
    echo "first[$l]"
    /usr/bin/true
    if read l2; then echo "second[$l2]"; else echo "second=EOF"; fi
    return
  done
}
F < f
  • rubash: first[l1] then second=EOF
  • GNU bash 5.3: first[l1] then second[l2]

What triggers it and what does not

Loop reads a 4-line file, /usr/bin/true (external) in the body unless noted:

# Shape rubash GNU bash 5.3
1 function + F < f call-site redirect count=1 ❌ count=4 ✅
2 subshell ( ... ) < f 4 ✅ 4 ✅
3 group { ...; } < f 4 ✅ 4 ✅
4 top-level while ...; done < f 4 ✅ 4 ✅
5 redirect written inside the function body 4 ✅ 4 ✅
6 function + call-site redirect, next read observed EOF ❌ next line ✅
7 function + call-site redirect, builtin true in the body 4 ✅ 4 ✅

So the trigger is exactly function + call-site redirect + external command in the body. Builtins do not trigger it, and body-internal redirects, group redirects, top-level redirects and subshells are all fine.

Additional observations:

  • The first iteration reads correctly: the redirect is live at function entry and dies at the child spawn.

  • The outer stdin is unaffected — after the function returns, the caller's own stdin is intact:

    printf 'O1\nO2\nO3\n' | sh -c '
      F () { while read l; do echo "F[$l]"; /usr/bin/true; done; }
      F < f
      while read l; do echo "OUT[$l]"; done'
    # prints F[l1], then OUT[O1], OUT[O2], OUT[O3]
  • Both invocation modes are affected: sh -c and bash -c behave identically (checked with the Niubash 1.4.0 sh.exe/bash.exe launchers pointing at niu.exe, and with a directly built engine binary).

Impact

  • This is what makes the upstream git test suite unusable on such a host: test_oid_cache <"$TEST_DIRECTORY/oid-info/hash-info" is a function called with a call-site redirect whose body runs external commands, so only the first line is read and every test then fails with undefined key 'zero' (ok=0 notok=0).
  • Real-world scripts are equally exposed: any func < file whose body calls external commands (e.g. git hooks) silently processes only the first input line.

Version / bisect notes

Activity

  1. caomengxuan666 commented on Oct 16, 2026

    @caomengxuan666
    MemberAuthor

    Additional datapoint: still reproduces on current master.

    • Engine built from 6e9b2b04 (master HEAD, cargo build --release), both modes:
      • F () { while read l; do echo "iter[$l]"; /usr/bin/true; done; }; F < f -> prints iter[l1] only
      • after the spawn: first[l1] then second=EOF
      • control { while ...; done; } < f -> 4/4 correct
    • Also reproduces on 754ba71b (the parent of 52b303eb) and in Niubash 1.4.0 as shipped (sh.exe/bash.exe -> niu.exe), so this is a long-standing defect, not a recent regression.
  2. added
    bug
    P1-high
    area:fd
    on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P1-higharea:fdbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions