Skip to content

[Core] tolerate unaccessible PID during child processes walk - #9806

Merged
aylei merged 4 commits into
masterfrom
worktree-subprocess-eacces-fix
Jun 8, 2026
Merged

aylei merged 4 commits into
masterfrom
worktree-subprocess-eacces-fix

Conversation

@aylei

@aylei aylei commented Jun 5, 2026 •

Copy link
Copy Markdown
Collaborator

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.

...
Using SkyPilot API server: http://127.0.0.1:46580
psutil.Process.children() hit AccessDenied (pid=6038 uid=0 exe=?); falling back to /proc walk that ignores unreadable PIDs.
psutil.Process.children() hit AccessDenied (pid=6038 uid=0 exe=?); falling back to /proc walk that ignores unreadable PIDs.
psutil.Process.children() hit AccessDenied (pid=6038 uid=0 exe=?); falling back to /proc walk that ignores unreadable PIDs.
psutil.Process.children() hit AccessDenied (pid=6038 uid=0 exe=?); falling back to /proc walk that ignores unreadable PIDs.
psutil.Process.children() hit AccessDenied (pid=6038 uid=0 exe=?); falling back to /proc walk that ignores unreadable PIDs.
psutil.Process.children() hit AccessDenied (pid=6038 uid=0 exe=?); falling back to /proc walk that ignores unreadable PIDs.
psutil.Process.children() hit AccessDenied (pid=6038 uid=0 exe=?); falling back to /proc walk that ignores unreadable PIDs.
psutil.Process.children() hit AccessDenied (pid=6038 uid=0 exe=?); falling back to /proc walk that ignores unreadable PIDs.
psutil.Process.children() hit AccessDenied (pid=6038 uid=0 exe=?); falling back to /proc walk that ignores unreadable PIDs.
....
πŸ“‹ Useful Commands
Job ID: 1
β”œβ”€β”€ To cancel the job:		sky cancel eacces-vfinal 1
β”œβ”€β”€ To stream job logs:		sky logs eacces-vfinal 1
└── To view job queue:		sky queue eacces-vfinal
Cluster name: eacces-vfinal
β”œβ”€β”€ To log into the head VM:	ssh eacces-vfinal
β”œβ”€β”€ To submit a job:		sky exec eacces-vfinal yaml_file
β”œβ”€β”€ To stop the cluster:	sky stop eacces-vfinal
└── To teardown the cluster:	sky down eacces-vfinal

The exe is captured in best effort manner.

Tested (run the relevant ones):

  • Code formatting: install pre-commit (auto-check on commit) or bash format.sh
  • Any manual or new tests for this PR (please specify below)
  • All smoke tests: /smoke-test (CI) or pytest tests/test_smoke.py (local)
  • Relevant individual tests: /smoke-test -k test_name (CI) or pytest tests/test_smoke.py::test_name (local)
  • Backward compatibility: /quicktest-core (CI) or pytest tests/smoke_tests/test_backward_compat.py (local)

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread sky/utils/subprocess_utils.py Resolved
@aylei
aylei marked this pull request as ready for review June 5, 2026 06:52
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.
@aylei

aylei commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator Author

/quicktest-core
/smoke-test -k cancel

@aylei
aylei requested a review from Michaelvll June 5, 2026 07:08
@aylei

aylei commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator Author

/quicktest-core
/smoke-test -k cancel

@Michaelvll Michaelvll left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @aylei! Please help make sure there is no accidental killing of some process that are supposed to be running

Comment thread sky/utils/subprocess_utils.py Resolved
Comment thread sky/utils/subprocess_utils.py Resolved
Signed-off-by: Aylei <rayingecho@gmail.com>
@aylei

aylei commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator Author

/quicktest-core
/smoke-test -k cancel

@aylei

aylei commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator Author

/smoke-test --kubernetes

@aylei
aylei merged commit c9686bd into master Jun 8, 2026
21 checks passed
@aylei
aylei deleted the worktree-subprocess-eacces-fix branch June 8, 2026 03:37
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.

2 participants