Repository navigation
[Core] tolerate unaccessible PID during child processes walk - #9806
Conversation
psutil.Process.children(recursive=True) builds the global {pid: ppid}
map by reading /proc/<N>/stat for every visible PID and aborts on the
first PermissionError. On hardened-host substrates where some
/proc/<N>/stat reads return EACCES (custom SELinux modules, eBPF LSM
agents like Falco/Tetragon), this turns into a hard failure of
kill_process_daemon (called from log_lib.run_with_log) and
kill_children_processes (called from teardown / recovery paths),
which then propagates and:
- fails managed-job controller polls as FAILED_CONTROLLER, and
- skips the subsequent proc.wait()/kill calls, leaking zombies and
leaving cloud resources unkilled during cleanup.
Walk /proc directly with per-PID error tolerance: PIDs that fail with
PermissionError are skipped, their descendants are unreachable and not
killed, but every PID we could enumerate is still cleaned up. One
aggregated warning per call captures pid/comm/SELinux-label of the
denied entries to aid future diagnosis. Existing behaviour
(NoSuchProcess swallowed via FileNotFoundError/ProcessLookupError) is
preserved. Non-Linux platforms fall back to psutil.
Tested:
- Added unit tests covering: clean tree enumeration, EACCES on a
mid-tree PID (sibling subtrees still enumerated, denied PID captured
in the diag list), and PID that vanishes mid-walk.
- Integration check that kill_process_daemon does not raise and that
kill_children_processes still SIGKILLs the explicit parent when the
/proc walk yields no children.
- All 17 existing subprocess_utils tests pass.
There was a problem hiding this comment.
Code Review
This pull request introduces a resilient mechanism for retrieving child processes to prevent cleanup failures caused by AccessDenied errors (e.g., due to hardened-host LSM policies). It adds _safe_children as a tolerant wrapper around psutil.Process.children that falls back to a manual /proc walk on Linux when access is denied. The review feedback points out that the fallback walk implements a DFS instead of the documented BFS, and suggests using collections.deque to achieve a true BFS. Additionally, it recommends catching psutil.Error instead of only psutil.NoSuchProcess when instantiating child processes to prevent potential permission crashes during the walk.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Addressing review feedback on _fallback_children:
- The comment claimed BFS but the stack.pop() loop is DFS. Update the
comment to call this out and clarify that it matches upstream
psutil.Process.children(recursive=True), which uses the same
stack-based pattern. Order does not matter for the callers
(kill_process_daemon / kill_children_processes both walk the whole
subtree to kill it).
- Broaden the catch around psutil.Process(child_pid) from
psutil.NoSuchProcess to psutil.Error. We only enter this fallback
because the host LSM is actively denying introspection; the per-PID
instantiation can plausibly hit AccessDenied or ZombieProcess too.
Dropping the offending PID and continuing the walk is better than
propagating and aborting cleanup of the rest of the subtree.
Tested: existing 19 unit tests in test_subprocess_utils.py still pass.
|
/quicktest-core |
|
/quicktest-core |
Michaelvll
left a comment
There was a problem hiding this comment.
Thanks @aylei! Please help make sure there is no accidental killing of some process that are supposed to be running
Signed-off-by: Aylei <rayingecho@gmail.com>
|
/quicktest-core |
|
/smoke-test --kubernetes |
Tolerate unaccessible PID when walking the process tree.
Repro script: https://gist.github.com/aylei/66ec8c18d0f70856917a9b64170fdf6d
Verified thataccess dined error on PID will be tolerated and logged.
The exe is captured in best effort manner.
Tested (run the relevant ones):
bash format.sh/smoke-test(CI) orpytest tests/test_smoke.py(local)/smoke-test -k test_name(CI) orpytest tests/test_smoke.py::test_name(local)/quicktest-core(CI) orpytest tests/smoke_tests/test_backward_compat.py(local)