Skip to content

[bug] Numbered read-write redirect (3<>file) on an external command is not delivered to the spawned child (EBADF) #2298

Closed

Description

@caomengxuan666

Summary

A numbered read-write redirect (3<>file) on an external command is not delivered to the spawned child: the child sees EBADF at that slot. The in-process faces work (builtin echo ... >&3 with 3<> writes fine; the script-child face was fixed by #2261), and a plain write-only 3>file delivers to spawned children fine — only the read-write endpoint class is dropped on the spawn face.

Found by the collector batch for PR #2296 (#2286's pin-harness repair): with the pin's observer corrected to a real spawned process, the pin's long-standing premise ("numbered 3<> on an external command already works") proved false — its old green was hollow twice over (invalid ENOEXEC observer over a broken face).

Reproducer (verified 2026-10-31, master c537d12 vs GNU bash 5.3.20)

sh -c 'echo via3 >&3' 3<>num.txt; echo rc=$?; cat num.txt
  • GNU: rc=0, num.txt = via3
  • rubash: rc=1, num.txt empty; child reports write error: Bad file descriptor (nested rubash) / I/O error (sh)

Controls on rubash, all correct: echo via3 >&3 builtin with 3<> (direct face) → writes; sh -c 'echo via3 >&3' 3>num.txt (write-only spawn) → writes.

Expected

GNU do_redirections delivers the opened read-write file description at the numbered slot in the child (redirections are applied in the forked child before exec; a <> open is O_RDWR).

Notes for the fix

The gap is in the external spawn-delivery layer's endpoint classification for read-write File endpoints (the walk/applier path that handles numbered outputs — cf. #2277/#2278's delivery ladder; plain outputs get through, read-write ones do not). Pins: the corrected control pin already exists on PR #2296 (tests/issue2261 target) and must go green; add pins under this issue's number for the read direction too (child reads a pre-seeded file through 3<>), a 3<> on /dev/null-class targets if the classification touches them, and the two controls above.

Destination

Fix cluster dispatched 2026-10-31 (fix-numbered-rw-spawn). PR #2296 (#2286) merges after this lands and its batch re-verifies.

Activity

  1. added this to the 1.5.0 milestone on Oct 30, 2026
  2. added
    bug
    gnu-baseline
    P2-medium
    area:fd
    on Oct 30, 2026
  3. caomengxuan666 commented on Oct 31, 2026

    @caomengxuan666
    MemberAuthor

    Fixed by PR #2301 (merge commit 872519e).

    Root cause, refined by the fix cluster's fdinfo evidence: numbered <> entries were not dropped — they were delivered read-only. The numbered walk in apply_external_fd_delivery classified RedirectKind::ReadWrite with the input kinds and opened via the O_RDONLY ladder (child fd flags 0100000 vs GNU's 0100002 O_RDWR), so reads worked and only writes hit EBADF — which is how the defect hid behind the old pin's hollow green. The fix opens <> entries through the classified readwrite ladder (GNU make_cmd.c:682 / redir.c:725) and stamps both env channels with the single dup so the pre_exec pin delivers an O_RDWR descriptor; stash-backed entries keep the read-only stamp.

    Verification: collector scrubbed-fd batch — issue2298 pins 8/8 (write incl. O_CREAT, nested write, read, one-description shared-offset in both directions, controls), controls issue2272 24/24, issue2278 13/13, issue2255 14/14; composed head CI all green; composition verified as exactly the PR's two files.

    Follow-ups from the same REPORT, filed separately: #2304 (numbered /dev/null redirects skipped on the spawn face, all kinds). PR #2296's corrected issue2261 control pin exercises this face and is being re-verified now that this has landed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P2-mediumarea:fdbuggnu-baseline

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions