jcode icon indicating copy to clipboard operation
jcode copied to clipboard

[bug] Bash reports exit 120 after a successful command that spawned a detached child

Open Aether0Pneuma opened this issue 3 weeks ago • 6 comments

Summary

On Windows, the bash tool can report exit 120 for a Python command that has already completed successfully, when that command spawns a detached background child. The action's own effects are present (files written, commit/push done, state updated, lease released), but the tool reports failure. The same command run under a normal subprocess with captured stdout exits 0.

Environment

  • jcode v0.99.1 (7206efd0c)
  • Windows, cmd.exe shell, Python 3.13
  • Detached child is started with subprocess.Popen(..., creationflags=CREATE_BREAKAWAY_FROM_JOB | DETACHED_PROCESS, stdout=<log file>, stderr=STDOUT)

Expected

A completed command returns its own exit status. A detached child that outlives it must not change the caller's status.

Actual

The command returns 120. stderr shows:

Exception ignored on flushing sys.stdout:
OSError: [Errno 22] Invalid argument

The work is complete despite the code. Running the same CLI from a normal Python subprocess.run(..., capture_output=True) returns 0.

Minimal evidence of the flush mechanism

import subprocess, sys
p = subprocess.Popen([sys.executable, "-c", 'print("x"); raise SystemExit(0)'],
                     stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdout.close()          # reader gone before the child flushes
print(p.wait())           # 120
print(p.stderr.read())    # Exception ignored on flushing sys.stdout: OSError: [Errno 22] Invalid argument

This reproduces CPython's exit 120 when its final stdout flush fails. In the real case the pipe appears to become invalid while a detached child is alive.

What did not help

Wrapping the CLI entry point so it flushes stdout/stderr and replaces a broken stream with os.devnull prevents 120 for normal returns and for --help/SystemExit, but the delivery command still returns 120 under the jcode bash tool when it spawns the detached child. This points at the tool's job/pipe handling of the detached child rather than the CLI's Python code.

Impact

Automation that runs a CLI through jcode bash sees a false failure after a successful operation, and may retry or abort work that is already complete.

Aether0Pneuma avatar Nov 12 '26 09:11 Aether0Pneuma

I can investigate this on Windows. Could you share a minimal version of the command that reproduces the exit 120 through Jcode, including how the detached child is launched and how long it stays alive? I reproduced the standalone Python example on Windows: closing the stdout reader produces exit 120 and the flush error. That verifies the Python mechanism but not the Jcode-specific trigger.

Related PR #2006 changes Windows background process handling and cancellation using job objects, but I have not verified whether it changes this failure.

Aafimalek avatar Nov 13 '26 08:11 Aafimalek

Hi Aafimalek, thanks for digging into this. Fair question, and I have to admit the minimal repro is exactly the part I could not get down.

What I see, on jcode v0.99.1 (7206efd), Windows, cmd.exe, Python 3.13: python scripts/slot.py deliver ..., run from a git worktree. It takes 60 to 90 seconds (commit, push, close the ticket, release the lease), and near the end it starts a maintenance process detached on purpose:

CREATE_NO_WINDOW | CREATE_NEW_PROCESS_GROUP | CREATE_BREAKAWAY_FROM_JOB

stdin is DEVNULL, stdout and stderr go to a log file, and the child can live for minutes. jcode reports exit 120. The work is all done: the branch is pushed, the ticket is closed, the lease is released. The same CLI through subprocess.run(..., capture_output=True) returns 0, and our release test (test_session_slots::test_release_cli_maintains_only_its_valid_lease) is green.

I did wrap the CLI entry point the way the issue describes, flushing stdout/stderr and replacing a broken stream with os.devnull. That fixed the standalone cases and --help, but not this command.

Things I tried to shrink it, all through the bash tool, all exit 0:

  • Popen with CREATE_BREAKAWAY_FROM_JOB | DETACHED_PROCESS, child sleeps 5 s, stdout to a file
  • the three flags above, same as our code
  • a child inheriting the caller's stdout pipe, living 20 s
  • a silent command running 95 s
  • a breakaway child, then the caller keeps printing once a second for 20 s

So "detached child outlives the caller" alone does not trigger it here. I am not blaming the tool either; I just cannot narrow it further without a real delivery.

Two corrections to my original report: our code does not use DETACHED_PROCESS at all, the flags are the three above, and the child is not a 5 second sleeper, it is a maintenance worker that can run for minutes.

You mentioned PR #2006, which touches the same ground: job object, preserving the real exit code when the shell has already exited but children are alive. Have you tried #2147 on that branch? If 120 goes away there, that answers it faster than anything I can send. If it does not, I can re-run a real delivery with stdout instrumentation and send the trace, but that needs a real delivery, so give me some time.

Aether0Pneuma avatar Nov 13 '26 09:11 Aether0Pneuma

Thanks for the details. I cannot run your slot.py deliver workflow, so I cannot tell yet whether #2006 fixes the exit-120 case.

On Windows with Python 3.13, I tested #2006 head 991508f5 through the actual Jcode BashTool in both foreground and explicit background modes. In each, a short Python parent launched a child with the three reported flags and redirected the child's output to a file. The foreground output showed no nonzero exit code; the background task recorded exit 0. Both children finished, and the background child was still running after its parent exited. The six existing Windows process tests on this branch also passed. This is a narrower probe, not a reproduction of your 60–90 second delivery.

One distinction that may help: #2006 applies its job-object handling when run_in_background is explicitly set. The normal foreground path starts separately, including commands later promoted after a timeout. Do you know whether the failing call was explicitly started in background mode? If it was foreground or timeout-promoted, this PR does not exercise the same startup path. The instrumented real-delivery trace you offered would be helpful when you have a chance.

Aafimalek avatar Nov 13 '26 12:11 Aafimalek

Quick update, and it probably narrows this a lot.

The failing call was a plain foreground bash command, not run_in_background. I also passed an explicit timeout of 2,400,000 ms, which the tool clamps to 600,000. The run itself took about 89 seconds, so it never hit the 120 s default, never hit the clamp, and was never promoted. Which means #2006 does not touch the path we are on, and that matches your two probes coming back 0.

So the interesting path is execute_foreground in crates/jcode-app-core/src/tool/bash.rs: the child gets Stdio::piped() for stdout and stderr, the tool waits on child.wait(), then drains both pipes to EOF. If the reading side goes away before the Python parent finishes, its final flush fails and CPython exits 120. That is the only thing I can see that fits both the exit code and the OSError: [Errno 22] on flush.

I am running a real release with sys.stdout/sys.stderr instrumentation now (a wrapper that logs every write, every flush, the fd state, and the moment the fd stops being usable) and will post the trace. Might take a bit, it is a real lease/release cycle.

Aether0Pneuma avatar Nov 13 '26 12:11 Aether0Pneuma

Update, and it is a negative result: I could not reproduce the 120 on this build at all.

I ran two real full deliveries through the bash tool on v0.99.1 (7206efd), Windows, cmd.exe. One added a harmless marker function, the other reverted it. Each took about 90 seconds and went through commit, push, Linear close, lease release and the detached maintenance start. Both returned exit 0.

I had a sitecustomize wrapper on PYTHONPATH for both runs: it logs every sys.stdout/sys.stderr write and flush, watches fd 1/2 with fstat every 100 ms, and traces os.close/os.dup2 for fd 1/2. Across both deliveries there is no RAISED, no became invalid, no close or dup2 on 1 or 2. The pipe stayed healthy the whole time.

So on this machine and this build the trigger did not show up, even on the exact path: slot.py deliver, about 90 seconds, detached maintenance child started with the three flags and stdout to a log file. The earlier 120 was real, same command, 2026-11-12, but I cannot reproduce it now. It could be something environment-specific on my side (Editor state, load, a transient job or pipe race) or a change between then and now.

If it helps, I can send the wrapper and the exact command line so you can run the same instrumentation on your side. Otherwise I am fine with closing this, or keeping it open as "not reproducible" in case someone hits it again. Your call.

Aether0Pneuma avatar Nov 13 '26 12:11 Aether0Pneuma

Thanks both for the careful narrowing. Triage: not reproducible at the moment, by the reporter's own latest runs. Leaving this open as needs-info. If it happens again, the most useful details are the exact command line, whether the call was foreground, and the stdout wrapper trace from that run.


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

1jehuang avatar Nov 13 '26 15:11 1jehuang