Fix flaky `test_stopping_a_command_kills_its_descendants` by treating a descendant reaped mid-read as gone

Merged
DS
22d ago
+2-1

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

Closes #13383

TestRealBubblewrap.test_stopping_a_command_kills_its_descendants[timeout] failed in the merge group of #13356 (job) with ProcessLookupError: [Errno 3] No such process.

Root cause. After the stop, the test waits for the killed grandchild by reading /proc/<pid>/stat until the file is gone or the state is Z, and catches only FileNotFoundError. When the host's init reaps the grandchild after Path.read_text opens the file and before it reads it, the read fails with ESRCH: Linux's proc_single_show in fs/proc/base.c returns -ESRCH once get_pid_task finds no task, and Python raises that as ProcessLookupError. A reaped grandchild is the outcome the test wants, so the loop now treats ProcessLookupError like FileNotFoundError.

Reproduction. Forced ordering: a pytest plugin makes pytest a child subreaper, so the orphaned grandchild stays a zombie until the plugin reaps it between the test's open and read. This box can't create user namespaces, so a pass-through bwrap stand-in ran the commands; CI runs the real bwrap.

[timeout][cancel]
Without the fix (a553eb969)fails, ProcessLookupErrorfails, ProcessLookupError
With the fixpassespasses
Plugin and bwrap stand-in
# force_reap.py, loaded with `-p force_reap`
import ctypes, os, pathlib, re

_STAT = re.compile(r'/proc/(\d+)/stat')
_read_text = pathlib.Path.read_text


def _forced_read_text(self, *args, **kwargs):
    match = _STAT.fullmatch(str(self))
    if match is None:
        return _read_text(self, *args, **kwargs)
    with open(self, encoding=kwargs.get('encoding')) as file:
        os.waitpid(int(match.group(1)), 0)  # reap the adopted orphan between the open and the read
        return file.read()


def pytest_configure(config):
    assert ctypes.CDLL(None).prctl(36, 1, 0, 0, 0) == 0  # PR_SET_CHILD_SUBREAPER
    pathlib.Path.read_text = _forced_read_text
#!/bin/sh
# `bwrap` stand-in: applies --chdir and --setenv, skips the isolation options, execs the command.
while [ $# -gt 0 ]; do
  case $1 in
    --) shift; break ;;
    --die-with-parent|--unshare-*|--new-session|--clearenv|--as-pid-1) shift ;;
    --cap-drop|--seccomp|--dev|--proc|--tmpfs|--unsetenv|--dir|--remount-ro) shift 2 ;;
    --chdir) cd "$2" || exit 1; shift 2 ;;
    --setenv) export "$2=$3"; shift 3 ;;
    --ro-bind|--bind|--ro-bind-try|--bind-try|--dev-bind|--dev-bind-try|--symlink) shift 3 ;;
    *) break ;;
  esac
done
exec "$@"

Other /proc reads in tests/harness/ were checked and left as is: e2b_sandbox/test_backend.py::_exited already catches ProcessLookupError, and the Docker, BoxLite and OpenSandbox live probes treat a failed read as a gone process.

What changes for existing users: nothing; test-only.

PR decisions

2026-11-13 · root cause: ESRCH from a reaped descendant's /proc stat read · iter -

  • Decision: Catch ProcessLookupError beside FileNotFoundError in the test's zombie-wait loop; test-only fix
  • Why: Reading an opened /proc//stat after the task is reaped returns -ESRCH (fs/proc/base.c proc_single_show); local probe reproduced ProcessLookupError
  • Source: https://github.com/pydantic/pydantic-ai/issues/13383
  • Session: b309f9af-ecac-4bb5-a0a4-c9a9927ec071

2026-11-13 · sibling /proc readers audited, none changed · iter -

  • Decision: e2b _exited already catches both; docker/boxlite/opensandbox shell probes treat a read error as gone; render_sandbox _identity reads under the reaping lock; modal live pgid probe not folded (live-only, not in CI, unreproduced)
  • Why: Root AGENTS.md: extend a fix only to surfaces where the defect reproduces
  • Source: https://github.com/pydantic/pydantic-ai/issues/13383
  • Session: b309f9af-ecac-4bb5-a0a4-c9a9927ec071

2026-11-13 · local verification without real bwrap · iter -

  • Decision: Box cannot run bwrap (AppArmor restricts unprivileged userns, no sudo); verify via pass-through bwrap shim with forced reap-between-open-and-read, real bwrap in CI
  • Why: Flake-fix bar needs a forced reproduction that fails without the fix and passes with it
  • Source: https://github.com/pydantic/pydantic-ai/issues/13383
  • Session: b309f9af-ecac-4bb5-a0a4-c9a9927ec071

2026-11-13 · pre-push review 1 of 3 at 0d4468ce71d5d087284eba219a68ea892f216866 · iter -

2026-11-13 · pre-push review 1 of 3 at 0d4468ce71d5d087284eba219a68ea892f216866 · iter -

2026-11-13 · modal live pgid probe: skip · iter -

  • Decision: Skip the modal_sandbox/test_modal_live.py pgid-probe sibling: no fold, no tracking issue
  • Why: Manager (relaying David): "an unreproduced finding gets no fold and no tracking issue"; not reproduced and not run in CI
  • Source: https://github.com/pydantic/pydantic-ai/pull/13385
  • Session: b309f9af-ecac-4bb5-a0a4-c9a9927ec071
  • Supersedes: sibling /proc readers audited, none changed

2026-11-13 · manager enqueues · iter -

  • Decision: Do not add the PR to the merge queue; at the merge bar, report gate state to the manager, who enqueues
  • Why: Manager (relaying David) instruction; overrides the flake-fix self-enqueue in pushing-commits-to-the-repo Who merges
  • Source: https://github.com/pydantic/pydantic-ai/pull/13385
  • Session: b309f9af-ecac-4bb5-a0a4-c9a9927ec071

2026-11-13 · PR Review Checks quota failure: option B · iter -

  • Decision: Apply douwebot on 0d4468ce7 before PR Review Checks passes; rerun PR Review Checks once at ~13:25Z; if douwebot also fails on quota, report and do not re-apply
  • Why: Manager (relaying David): both PR Review Checks attempts got 429 from every provider, no review ran; the flake ejects every author's queued PRs; option A (pr-review-checks:accepted) raised with David in parallel
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43423683463
  • Session: b309f9af-ecac-4bb5-a0a4-c9a9927ec071

2026-11-13 · douwebot quota failure on 0d4468ce7 · iter -

  • Decision: douwebot run failed on provider quota (429 on OpenCode Go and both Z.ai Coding Plans); label not re-applied, reported to manager
  • Why: Manager instruction: if douwebot fails on quota too, report it and don't re-apply
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43427239459
  • Session: b309f9af-ecac-4bb5-a0a4-c9a9927ec071

2026-11-13 · 13:25Z retry: PR Review Checks rerun plus douwebot re-apply · iter -

  • Decision: At 13:25Z rerun PR Review Checks and re-apply douwebot together; on another quota failure of either, report and stop; if both review clean, report gate state for the manager's enqueue; no pr-review-checks:accepted unless David's answer is relayed
  • Why: Manager (relaying David): provider quota expected to reset by 13:25Z
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43427239459
  • Session: b309f9af-ecac-4bb5-a0a4-c9a9927ec071

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

https://claude.ai/code/session_01HhQjZAMPqa9rWRmZdj4D22


Summary by cubic

Fixes the flaky test_stopping_a_command_kills_its_descendants test by treating a descendant reaped mid-read as gone.

The test's zombie-wait loop previously caught only FileNotFoundError when reading /proc/<pid>/stat. When a descendant is reaped between opening and reading the file, Linux returns ESRCH, which Python raises as ProcessLookupError, failing the test despite the descendant having exited as intended. The loop now treats ProcessLookupError the same as FileNotFoundError. Test-only change with no user-facing impact.