Skip to content

Background bash inherits the daemon's revoked stdin, killing any Python child (execute_background never sets stdin) #903

Closed

Description

@H5-O5

Summary

Commands started with run_in_background: true inherit the daemon's revoked stdin (fd 0). Any child that initialises a Python interpreter dies immediately with a fatal error, while the exact same command succeeds in the foreground.

execute_foreground and execute_background disagree about stdin: the foreground path sets it, the background path never mentions it, so the child inherits fd 0 from the jcode ... serve process — where it is (revoked), not merely closed.

Impact

The blast radius is much wider than "Python scripts", because the victim is anything that transitively starts a Python interpreter. In our case this hit four unrelated tools in a single day — a CLI that happens to be written in Python, a shell script that calls it, an inline python3 -c, and a .py helper — none of which look Python-related from the call site.

Worse, the crash is often swallowed and re-reported as a plausible domain error. One of our scripts probes authentication as its first step; when that probe died this way, the script concluded "authentication expired, run <cli> auth login" and printed it as the recommended next step. Authentication was in fact valid. So the failure mode is not just "the command fails" — it can hand the user a confident, wrong next action.

Reproduction

Run this with run_in_background: true:

echo "A. fd 0 of this background shell:"
lsof -p $$ -a -d 0 | tail -1

echo "B. plain python3:"
python3 -c "print('hello from python')" 2>&1 | head -3

echo "C. same command, with a valid stdin:"
: > /tmp/.empty
python3 -c "print('hello from python')" < /tmp/.empty

Observed (jcode v0.68.0, macOS arm64; the same code is present on master @ 8c9cf2f / v0.75.3):

A. fd 0 of this background shell:
bash    20061 user    0                             (revoked)

B. plain python3:
Fatal Python error: init_sys_streams: can't initialize sys standard streams
Python runtime state: core initialized
OSError: [Errno 9] Bad file descriptor

C. same command, with a valid stdin:
hello from python

The same three steps in the foreground print hello from python at step B.

Confirming where fd 0 comes from — the background shell's parent is the server process, and its fd 0 is revoked:

$ lsof -p 17468 -a -d 0,1,2      # /opt/homebrew/bin/jcode --provider auto serve
jcode  17468  user  0            (revoked)
jcode  17468  user  1w  CHR  3,2  /dev/null
jcode  17468  user  2   PIPE ...

Root cause

crates/jcode-app-core/src/tool/bash.rs

Foreground (execute_foreground, ~L787) — stdin is handled:

command
    .kill_on_drop(true)
    .stdout(Stdio::piped())
    .stderr(Stdio::piped());

if has_stdin_channel {
    command.stdin(Stdio::piped());
}

Background (execute_background, ~L1163) — stdin is never mentioned:

cmd.kill_on_drop(true)
    .stdout(Stdio::piped())
    .stderr(Stdio::piped());

std/tokio Command defaults unspecified stdio to inherit, so the child receives the server's revoked fd 0. CPython's init_sys_streams calls fstat on fd 0 during startup, gets EBADF, and aborts before running any user code — hence the failure happens even for python3 -c "print(1)".

Note this is not the same as having no stdin: a closed-and-revoked descriptor is worse than /dev/null, which is a perfectly valid fd that simply reads EOF.

A correct pattern already exists a few hundred lines above, in execute_reload_persistable_foreground (~L971):

cmd.stdin(Stdio::null()).stdout(stdout).stderr(stderr);

which suggests the background path simply omits the line rather than doing so deliberately.

Suggested fix

cmd.kill_on_drop(true)
    .stdin(Stdio::null())
    .stdout(Stdio::piped())
    .stderr(Stdio::piped());

Stdio::null() keeps the intended semantics for background work (a background command should not expect interactive input, and this also prevents it from blocking forever on a read), while giving the child a valid descriptor so runtimes that inspect fd 0 at startup — Python being the strictest — initialise normally.

Happy to send a PR if that would help; the change is one line plus a regression test asserting that a background command which reads stdin terminates instead of aborting.


Drafted with Claude Code.

Activity

  1. H5-O5 commented on Aug 11, 2026

    @H5-O5
    Author

    Small follow-up with a concrete test approach, in case it saves whoever picks this up some time.

    I had a regression test drafted before deciding to leave the fix to upstream. The tricky part is that the bug only shows when the parent has an fd 0 that the child must not inherit — so the test has to manufacture that situation rather than rely on however the test binary happens to be launched. dup2-ing a temp file onto fd 0 for the duration of the test does the job:

    #[cfg(unix)]
    #[tokio::test]
    async fn test_background_command_gets_null_stdin_instead_of_inherited_fd() {
        // Give this process a real fd 0 pointing at a marker file, so "inherited"
        // and "/dev/null" are distinguishable from inside the child.
        let saved_stdin = unsafe { libc::dup(0) };
        unsafe { libc::dup2(inherited.as_raw_fd(), 0) };
    
        // Background command asserts on its own stdin, then reads it:
        //   if [ /dev/stdin -ef /dev/null ]; then echo STDIN_IS_NULL
        //   else echo STDIN_INHERITED; fi
        //   cat; echo CAT_EXIT=$?
        ...
        // restore fd 0 from saved_stdin at the end
    }

    Two assertions worth keeping separate, since they catch different regressions:

    1. STDIN_IS_NULL — the child did not inherit the parent's descriptor.
    2. CAT_EXIT=0 with the task reaching a terminal state — a background command that reads stdin terminates (EOF) instead of hanging forever. This is the property that would break if someone "fixed" it with Stdio::piped() and never closed the write end.

    Worth noting for anyone reproducing: the failure does not require Python code that touches stdin. python3 -c "print(1)" is enough, because CPython inspects fd 0 during init_sys_streams before any user code runs. That is also why the crash looks unrelated to stdin at the call site.


    Drafted with Claude Code.

  2. added
    bug
    priority: high
    triage: reproducible
    autonomous: clear
    regression
    triage: fixed-pending-release
    on Aug 12, 2026
  3. 1jehuang commented on Aug 12, 2026

    @1jehuang
    Owner

    Implemented null stdin for background commands and added a regression test proving reads observe EOF instead of inheriting the daemon descriptor, in #907. The focused test and integrated TUI build pass.


    — Jcode agent (automated triage), on behalf of @1jehuang

  4. added a commit that references this issue on Aug 12, 2026
    c702ec8
  5. added a commit that references this issue on Aug 15, 2026
    a8e0afd
  6. added 5 commits that reference this issue on Aug 30, 2026
    2d87196
    30f430a
    22f8502
    27e45b5
    ecd462a
  7. added a commit that references this issue on Sep 18, 2026
    344cab9
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

    autonomous: clearbugpriority: highregressiontriage: fixed-pending-releasetriage: reproducible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions