Skip to content

feat: implement kill_process_tree to recursively terminate processes and their descendants#35163

Merged
feici02 merged 3 commits intomainfrom
fix/some-win-run-error
Apr 21, 2026
Merged

feat: implement kill_process_tree to recursively terminate processes and their descendants#35163
feici02 merged 3 commits intomainfrom
fix/some-win-run-error

Conversation

@tomchon
Copy link
Copy Markdown
Contributor

@tomchon tomchon commented Apr 16, 2026

This pull request improves the reliability of process termination in the Windows test runner by ensuring that all child processes are properly killed when a test is interrupted or times out. The main enhancement is the addition of a kill_process_tree function, which recursively kills a process and all its descendants, addressing issues where child processes could be left running.

Process management improvements:

  • Added a new kill_process_tree function to recursively terminate a process and all its child processes using psutil, ensuring no orphaned subprocesses remain after test interruption or timeout.
  • Replaced all direct calls to current_process.kill() with kill_process_tree(current_process) in the test execution and cleanup logic, so that process trees are reliably cleaned up on timeout, interruption, or manual stop. [1] [2]

Copilot AI review requested due to automatic review settings April 16, 2026 14:09
@tomchon tomchon requested a review from a team as a code owner April 16, 2026 14:09
Copy link
Copy Markdown
Contributor

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

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 kill_process_tree function to recursively terminate a process and its descendants, specifically addressing issues on Windows where child processes of a shell remain active after the shell is killed. The reviewer suggested several improvements to make the function more robust, including adding a None check for the process argument, handling psutil.AccessDenied exceptions during iteration, and calling proc.wait() to ensure the process is properly reaped. Additionally, a potential race condition involving the signal handler was identified.

Comment thread test/ci/run_win_cases.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves Windows CI test-runner cleanup by introducing a kill_process_tree helper (psutil-based) and using it instead of killing only the immediate subprocess, reducing the chance of orphaned child processes when tests timeout or are interrupted.

Changes:

  • Added kill_process_tree(proc) to recursively kill a subprocess and its descendants.
  • Replaced direct current_process.kill() calls with kill_process_tree(current_process) in interruption/timeout paths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/ci/run_win_cases.py
Comment thread test/ci/run_win_cases.py
Comment thread test/ci/run_win_cases.py Outdated
tomchon and others added 2 commits April 21, 2026 14:47
Copilot AI review requested due to automatic review settings April 21, 2026 08:49
Copy link
Copy Markdown
Member

@feici02 feici02 left a comment

Choose a reason for hiding this comment

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

I made following changes for this PR:

  • run ruff format
  • run ruff check --fix

@feici02 feici02 merged commit c0affc1 into main Apr 21, 2026
6 of 9 checks passed
@feici02 feici02 deleted the fix/some-win-run-error branch April 21, 2026 08:51
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the Windows CI test runner’s cleanup behavior by introducing a process-tree termination helper so that interrupt/timeout handling kills the full subprocess tree (not just the top-level cmd.exe created by shell=True).

Changes:

  • Added kill_process_tree(proc) using psutil to kill a process and its descendants.
  • Replaced direct current_process.kill() calls with kill_process_tree(current_process) in interrupt/timeout paths.
  • Included various small formatting/consistency updates (logging config, string quoting, wrapping).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/ci/run_win_cases.py
Comment on lines +28 to 33
# 终止当前子进程(使用 kill_process_tree 确保子进程也被清理)
if current_process is not None:
try:
current_process.terminate()
kill_process_tree(current_process)
except:
pass
Comment thread test/ci/run_win_cases.py
Comment on lines +154 to +158
for child in children:
try:
child.kill()
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
Comment thread test/ci/run_win_cases.py
Comment on lines +159 to +167
proc.kill()
gone, still_alive = psutil.wait_procs([parent] + children, timeout=5)
for p in still_alive:
try:
p.kill()
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
try:
proc.wait(timeout=3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants