Skip to content

fix(shell): wait for a killed process to vanish instead of a single liveness check (#21308) - #21370

Closed
mrveiss wants to merge 2 commits into
mainfrom
issue-21308-bounded-shell-deadline
Closed

mrveiss wants to merge 2 commits into
mainfrom
issue-21308-bounded-shell-deadline

Conversation

@mrveiss

@mrveiss mrveiss commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thinking Path

The assertion that failed (the child outlived the deadline) is the safety property of #20740, so the first question was whether the product or the test is wrong. The product sends SIGKILL to the whole process group and then reaps only the group leader (autobot_shared/subprocess_reap.py kill_and_reap, called from utils/bounded_subprocess.py _wait_bounded). Its contract (#20740 ACs: kill on expiry, a test shows a sleeping command killed at the bound) is "killed", and a non-leader member of the group cannot be reaped by the caller: it stays a zombie in the group until its new parent reaps it, so the product has no deterministic thing to wait on either.

The test checked liveness once, immediately, with os.kill(pid, 0). That call succeeds for a zombie, and for a process whose SIGKILL has been queued but not yet delivered, which is likely on a loaded worker. Sightings: the original report (killpg test), PR #21328 head 2a91f28 (shard 3/8), merge-train PR #21349 run 43176216525 shard 3/8, line 51 _alive(<pid>) True, one failure in ~7300 tests on a parallel worker.

What Changed

  • autobot-backend/utils/bounded_shell_default_20740_test.py: both single-shot checks (test_a_command_with_no_policy_is_killed_at_the_default_deadline, test_a_timed_out_command_kills_only_its_own_process_group) now poll /proc/<pid>/stat until the process is gone, bounded by the deadline value the test hands the runner (_DEADLINE_S). Gone means absent, a zombie (state Z), or a different process under the same pid (start time recorded by the child itself from its own stat line, builtins only).
  • New test_a_grandchild_the_shell_waits_on_dies_with_the_group.
  • Product code unchanged. No env var added.

Why this shape

Product wait was considered and rejected: the group keeps zombie members until their parent reaps them, so polling the group would wait on an init-side event the product does not control. A zombie is dead for the safety property. The new test would fail on a shell-only kill: a sleep 60 the shell is blocked on in wait would be orphaned and stay running (state S) past the poll bound, so it cannot pass by timing accident. A survivor is detected by state, not by elapsed time.

Verification

  • Cause evidence: run 43176216525, shard 3/8 log, failure at 2026-11-12T09:11:56Z, _alive(14027) True; source reading of kill_and_reap and the old _alive.
  • Not run locally; CI is the gate. Syntax check only. Whether the zombie or the pending-signal case produced the failure is not determined from the log (I don't know which).
  • The flake's absence needs CI evidence over several runs, hence Refs.

Model Used

Claude Sonnet 5.5

Refs #21308

@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 55 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between c5a3f23 and 5e5b587.

Files selected for processing: 1

  • autobot-backend/utils/bounded_shell_default_20740_test.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@mrveiss mrveiss left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Verdict: APPROVE @ f013bb4

Static review; not run.

Process tree: run_shell_bounded -> create_subprocess_shell(start_new_session=True) = sh -c "<cmd>", group leader (pgid == pid), non-interactive so no job control and every descendant stays in that group. <cmd> is sh -c 'record; sleep 60'; the recorded $$ is the inner sh, the leader or its direct child depending on whether the outer sh execs its last command. Nothing uses setsid/nohup. In the new test sleep 60 & is a true non-leader grandchild.

Diagnosis (product correct): bounded_subprocess.py:99-115 _wait_bounded: on TimeoutError finished stays False, so finally runs kill_and_reap(process, process_group=True). subprocess_reap.py kill_and_reap does an unconditional os.killpg(process.pid, SIGKILL) (no returncode guard on the group path), suppressing only ProcessLookupError (group already empty), then reaps the leader. The leader is still unreaped at kill time, so its pid is still the pgid; no pid-reuse window. No SIGTERM grace path, no early return. If the shell exits by itself at the deadline, the group is still killed (the group path ignores returncode). SIGKILL cannot be blocked, and delivery to another task is asynchronous. A zombie also answers kill(pid,0). So a correct product can fail the old one-shot _alive, and (A) is sufficient. Established: control flow, and the log (_alive(14027) True at 09:11:56, one failure in ~7300). Not established: zombie vs pending-signal, and whether the pid was dead afterwards (the log has no later probe). A real leak is not excluded by data, only by the code path. This matches #20740's AC ("kills the process on expiry; a test shows a sleeping command killed at the bound").

False green: bound = _DEADLINE_S = 1 s, polled after the run already returned; the children sleep 60 s. A shell-only kill leaves sleep 60 in state S with the same start time, so the poll returns False at 1 s and the assertion fails. Natural exit cannot mask it (60 s >> 1 s) in any of the three tests. Weakness, non-blocking: in the default-deadline and own-group tests, if the outer sh execs the inner sh the recorded pid IS the leader, so those two cannot distinguish shell-only from group kill. The new grandchild test is the discriminating one, and it does (real grandchild, wait keeps the shell alive until killed).

Stat line: $$ is expanded by the inner sh (it sits inside the outer single quotes), so it is the inner sh's own pid; > file binds to the echo only; in the new test $! is the sleep's pid. The start time is unchanged by exec. _stat_fields splits after the LAST ): state = index 0, starttime = index 19 (field 22). Correct. Fallback to ProcessLookupError on read handled.

False red: after the 1 s kill window a Z zombie counts as gone (right even where no init reaps). Non-blocking: reusing the 1 s deadline as the poll bound leaves little margin on the same loaded worker that caused the flake. Use a separate _GONE_WITHIN_S of ~5 s; false green is unaffected since the sleeps are 60 s.

Hygiene: test file only, 187 lines, no product change, Refs not Closes, no footer, labels/milestone set. Added runtime: one extra ~1 s test. /proc is Linux-only and the file has no skip markers (killpg/getpgid already POSIX-only); CI is Linux, fine. Non-blocking: on assertion failure the sleep 60 leaks for a minute (no finally kill); add one.

Own CI: gh pr checks returned no shard rows yet.

Blocking: none. Non-blocking: wider poll bound; cleanup in finally; optional product strengthening (bounded wait for the group to be empty) tracked on #21308.

@github-actions

Copy link
Copy Markdown
Contributor

Notice: 29 open PRs — past the runaway threshold (25)

This is not a request to defer this PR. There is no cap on open PRs (#19397) -- nothing blocks opening or pushing a PR.

This notice only means the count is high enough to be worth a glance for a runaway — something opening PRs in a loop, or a merge pipeline that has stalled so nothing is draining.

Currently open:

If the queue is draining normally, ignore this. Otherwise:

  1. Check whether CI is dispatching at all — see the ci-dispatch-watchdog status on these PRs
  2. Approved PRs land through the merge train the coordinator sequences (merge commits, never squash) -- check the open chore(merge-train) PR is moving, not each PR on its own
  3. Look for a loop opening near-identical PRs

Warn-only runaway detector — .github/workflows/pr-queue-gate.yml. It never blocks a merge.

@mrveiss

mrveiss commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Both findings addressed in cb8d2fc (test file only; not run locally; needs CI).

  1. Poll bound: new module constant _EXIT_POLL_BOUND_S = 5 replaces the reuse of the 1 s deadline in the single exit-poll helper (_gone_within_bound, used by three tests). No false pass is possible: every polled child sleeps 60 s, so a process the product failed to kill is still running at 5 s, and each caller asserts the poll's result after it returns, with no fall-through.

  2. Cleanup: _kill_leftover(pid, start) sends a best-effort SIGKILL (ProcessLookupError ignored) from a finally in the three tests that assert a child's death: default deadline, own process group, and grandchild. It checks the recorded start time first and never signals a gone, zombie or reused pid. The finally runs after the assertions, so the original failure still propagates.

@mrveiss mrveiss left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Verdict: APPROVE @ cb8d2fc

Scope: delta f013bb4..cb8d2fc, one file (bounded_shell_default_20740_test.py, +34/-11). The earlier diagnosis and analysis stand.

Poll bound: _EXIT_POLL_BOUND_S = 5 s. The loop uses loop.time() (monotonic), asyncio.to_thread for the /proc read and asyncio.sleep, so the event loop is never blocked. It returns False at the bound, and the assert runs on that boolean after the loop. Every child sleeps 60 s, so a surviving child reads as alive for the whole 5 s and a false pass is impossible. Worst case is about 6 s when the product is broken and milliseconds past the 1 s deadline when it is correct. No per-test timeout exists in the suite config: the only "timeout" match in pytest.ini is a comment, and pyproject.toml has none.

Cleanup (a)-(e):
(a) pid/start are read before the try, by _identity_from, which raises before any finally exists. No unbound name and no masking.
(b) _kill_leftover handles ProcessLookupError, and _is_gone handles a missing stat file. The remaining exposure is an empty or short stat read, which would raise IndexError. That is extremely unlikely for a pid this test started, and it would surface only on the failure path.
(c) The pid-reuse guard is sound. _is_gone compares the recorded start time, so a recycled pid is treated as gone and never signalled. A same-user pid also rules out PermissionError.
(d) On the success path the process is already gone, so _kill_leftover returns without sending a signal.
(e) The grandchild test cleans the grandchild sleep. If the product were broken, the shell is blocked in wait, and killing the sleep lets it exit, so nothing is left behind. In the two tests that clean the sh -c pid, a non-exec'ing sh could leave its own sleep 60 behind. It would exit by itself within 60 s.

Line length: line 192 (the grandchild docstring) is 121 columns. It is new in this PR, since main has no line over 120 and the grandchild test does not exist there. It was already 121 columns at f013bb4 (line 172). CI enforces max-line-length = 120 in .flake8, but that file's per-file-ignores list E501 for *_test.py, so flake8 will not fail it. Black does not rewrap docstrings.

Hygiene: signal and os are both used. _gone_within_bound is consistent at all three call sites, and no stale names remain. There is no comment-marker matching and no glob-like quoted strings.

Findings:

  • Non-blocking: wrap the 121-column docstring at line 192 to 120 or less.
  • Non-blocking: in _is_gone, treat an empty or short stat read as gone, or catch IndexError.
  • Non-blocking: in the two sh -c tests, the inner sleep 60 is not tracked if sh does not exec it.

CI at head: only "Detect changed paths" is pending, and no shard has completed.

Static review; not run.

@github-actions

Copy link
Copy Markdown
Contributor

✅ SSOT Configuration Compliance: Passing

🎉 No new hardcoded values of either class — ssot and other both block.

Known backlog in pipeline-scripts/hardcoded_values_baseline.txt is suppressed and tracked in #14371.

@mrveiss

mrveiss commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Boarded merge train #21373 (T114) at 9441e8f3f8. It lands with the train.

@mrveiss

mrveiss commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Landed on main through merge train #21373 as 394642f; member head cb8d2fc is an ancestor of main (the PR branch has since moved to 5e5b587, an auto-sync merge of main only, not in the train).

Content check on main (3-dot diff of the member head against main is empty): autobot-backend/utils/bounded_shell_default_20740_test.py has _is_gone :39, _gone_within_bound :52, _EXIT_POLL_BOUND_S = 5 :27, _kill_leftover :63, and the grandchild test test_a_grandchild_the_shell_waits_on_dies_with_the_group :191. Product code unchanged.

No revert of it on main. Issue #21308 stays open: the absence of the flake needs several subsequent CI runs.

@mrveiss mrveiss closed this Nov 12, 2026
timothybrush pushed a commit to timothybrush/AutoBot-AI that referenced this pull request Nov 12, 2026
@mrveiss mrveiss reopened this Nov 18, 2026
@mrveiss

mrveiss commented Nov 18, 2026

Copy link
Copy Markdown
Owner Author

Branch audit (zero-change deletion). This PR shows 0 changed files against main (head f0cd11a472): every change it carried is already in main. Closing and deleting the branch through this PR; it can be restored with the Restore branch button below.

@mrveiss mrveiss closed this Nov 18, 2026
@mrveiss
mrveiss deleted the issue-21308-bounded-shell-deadline branch November 18, 2026 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend bug Something isn't working phase:M4.6 Shared primitives in one place priority: high testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant