Skip to content

Bound ai-accounts stderr drain after the command exits - #1721

Merged
mergify[bot] merged 2 commits into
mainfrom
lean178/1660-bound-stderr-drain
Nov 26, 2026
Merged

mergify[bot] merged 2 commits into
mainfrom
lean178/1660-bound-stderr-drain

Conversation

@eowca

@eowca eowca commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Bound ai-accounts stderr drain after the command exits

Context and goal

  • Context: The account wrapper streams a managed command's stderr and uses a complete error to decide whether replay is safe.
  • Problem: A descendant can keep stderr open after its leader exits, leaving the wrapper waiting indefinitely.
  • Goal: Return the leader's exit status after a one-second post-exit drain when the stream is still open, and allow replay only when the drain is complete.

Closes #1660

Acceptance

  • A local child inheriting stderr cannot keep the wrapper waiting after the leader exits.
  • The exit status and ordinary streaming and safe replay are preserved.
  • Partial stderr cannot trigger replay.

Base

main @ 4e1c0a4

Validation

Recorded for 59f6deab582e

  • uv run --group dev ruff check src tests: exit 0
  • uv run --group dev tach check: exit 0
  • uv run --python 3.14 python scripts/check_architecture_docs.py: exit 0
  • scripts/size_budget.sh: exit 0
  • uv run --group dev pytest -q: exit 0

Review

  • primary, claude, 0a875f9, approve

Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Architecture-Review: unchanged; the optional account shim keeps command and replay behavior and returns the known exit status if stderr remains open.
Co-Authored-By: Codex <noreply@openai.com>

@loopzero-feder-positronics loopzero-feder-positronics Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

loopzero primary review by claude on 0a875f98cd32fc1f240b35e754593178c618bf3f, 42.0s: approve (0 critical, 0 important, 3 suggestion)

Settings per native call (launch configuration and CLI claims, not backend attestation):

  • Call 1: requested model claude-opus-5-5 (effort medium); CLI-reported model claude-opus-5-5 (effort unknown); configured model claude-opus-5-5 (effort medium)

Native Claude output is retained in the restricted local review receipt.

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.

Comment thread tests/test_ai_accounts.py
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.

Comment thread tests/test_ai_accounts.py

def test_run_returns_after_leader_exits_with_inherited_stderr(helper):
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.

@eowca
eowca marked this pull request as ready for review November 26, 2026 09:27
@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@Mergifyio queue

@mergify

mergify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Merged into main as 3d70c5a at 09:34 UTC

5 min in the queue, 4 min of it CI.

So far

  • ✅ Entered the queue Thu 26 Nov, 09:28 · rule main · triggered by @eowca with the @mergifyio queue command
  • ✅ Passed · draft #1763
  • ✅ Merged 09:34 · 3d70c5a · squash
3 merge conditions met
  • github-review-approved · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required
  • any of · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required:
    • check-success = checks
    • check-neutral = checks
    • check-skipped = checks
  • any of · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required:
    • check-success = Loop-zero Eligibility
    • check-neutral = Loop-zero Eligibility
    • check-skipped = Loop-zero Eligibility

Times in UTC · dashboard · how the merge queue works

@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

No description provided.

@mergify
mergify Bot merged commit 3d70c5a into main Nov 26, 2026
13 checks passed
@mergify mergify Bot removed the queued label Nov 26, 2026
@eowca
eowca deleted the lean178/1660-bound-stderr-drain branch November 26, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bound ai-accounts stderr drain after the wrapped command exits

1 participant