Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion scripts/ai-accounts/ai-accounts
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,9 @@ def cmd_run(providers: str, argv: list[str], retry_exit: set[int] | None = None,
t = threading.Thread(target=pump, daemon=True)
t.start()
code = proc.wait()
t.join()
t.join(timeout=1)
if t.is_alive():
return code

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: A daemon pump still writing at shutdown can abort the interpreter and replace the leader's exit code

When the drain times out, cmd_run returns and sys.exit starts interpreter finalization while the daemon pump thread is still running. If a lingering descendant keeps writing stderr and the pump is blocked inside sys.stderr.buffer.write (for example because whoever reads the wrapper's stderr has stalled), it holds the BufferedWriter lock. During finalization, flush_std_files waits about 1s for that lock and then calls Py_FatalError("could not acquire lock for <stderr> at interpreter shutdown, possibly due to daemon threads"). The process then exits from SIGABRT (status 134) instead of returning the leader's code. That breaks the 'exit status is preserved' acceptance criterion in the same scenario this change targets. This only happens when the stderr consumer stalls, so the window is narrow. Smallest fix: have pump write with os.write(2, chunk) instead of the buffered sys.stderr.buffer, so the daemon never holds an interpreter-level I/O lock. Alternatively, call sys.stderr.flush() then os._exit(code) on the timed-out path.

text = b"".join(seen).decode("utf-8", "replace")
retryable = code != 0 and (retry_exit is None or code in retry_exit)
if (not retryable or attempt == 2 or not model or providers not in PROVIDERS
Expand Down
6 changes: 6 additions & 0 deletions tests/test_ai_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ def test_codex_requires_requested_model_evidence(helper, monkeypatch):
assert helper.active('codex') == 'main'


def test_run_returns_after_leader_exits_with_inherited_stderr(helper):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: No test checks that partial stderr blocks replay

The new test only checks that the wrapper returns exit code 7 when nothing would trigger replay. Nothing checks the third acceptance item: when the drain times out after a retryable exit whose partial stderr already matches USAGE_LIMIT_RE, exhausted must not be called and no second attempt may run. If someone later moves the t.is_alive() check below the retry decision, every test still passes. Add a case to the existing parametrized replay test (or an in-process cmd_run test with exhausted monkeypatched) where the leader prints the usage-limit text, starts a child that holds stderr open, and exits with a retryable code. Assert one run and no call to exhausted.

argv = [helper.sys.executable, helper.__file__, 'run', 'claude', '--', helper.sys.executable,
'-c', 'import subprocess, sys; subprocess.Popen(["sleep", "6"]); sys.exit(7)']

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The test leaves a sleep 6 process running after it finishes

The grandchild sleep 6 is never reaped or killed, so it outlives the test by several seconds. Under parallel or repeated runs this leaves stray processes behind. A shorter sleep (for example 3s, which still exceeds the 1s drain plus startup and stays under the 5s timeout) shrinks the leak. Running the leader in its own session and killing that process group at teardown would remove it completely.

assert helper.subprocess.run(argv, timeout=5).returncode == 7


@pytest.mark.parametrize('providers,explicit,retry_safe,runs,active', [
('claude,codex', False, False, 1, 'a'), ('claude', False, False, 1, 'b'),
('claude', False, True, 2, 'b'), ('claude', True, True, 1, 'a')])
Expand Down
Loading