Repository navigation
Description
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/.emptyObserved (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
H5-O5 commentedon Aug 11, 2026
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:Two assertions worth keeping separate, since they catch different regressions:
STDIN_IS_NULL— the child did not inherit the parent's descriptor.CAT_EXIT=0with 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 withStdio::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 duringinit_sys_streamsbefore any user code runs. That is also why the crash looks unrelated to stdin at the call site.Drafted with Claude Code.
1jehuang commentedon Aug 12, 2026
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
fix: null background command stdin (fixes 1jehuang#903)
chore: sync fork with upstream jcode (fresh merge onto current master) (
fix: null background command stdin (fixes 1jehuang#903)
fix: null background command stdin (fixes 1jehuang#903)
fix: null background command stdin (fixes 1jehuang#903)
fix: null background command stdin (fixes 1jehuang#903)
fix: null background command stdin (fixes 1jehuang#903)
fix: null background command stdin (fixes 1jehuang#903)