Clean vLLM process groups after parent exit

This commit is contained in:
2026-05-16 14:52:05 +08:00
parent 5a879a8592
commit cf9b8b3f68
2 changed files with 35 additions and 9 deletions

View File

@@ -401,27 +401,37 @@ def _wait_for_server_or_exit(
raise HttpClientError(f"Timed out waiting for {base_url}{healthcheck_path}: {last_error}")
def _process_group_exists(pgid: int) -> bool:
try:
os.killpg(pgid, 0)
return True
except ProcessLookupError:
return False
def _terminate_process_tree(process: subprocess.Popen[str], *, timeout_s: float = 30.0) -> None:
if process.poll() is not None:
return
try:
pgid = os.getpgid(process.pid)
except ProcessLookupError:
return
# Children can keep the session/process group alive after the vLLM API
# server exits. In that case the group id is still the original pid
# because the process was launched with start_new_session=True.
pgid = process.pid
try:
os.killpg(pgid, signal.SIGTERM)
except ProcessLookupError:
return
deadline = time.monotonic() + timeout_s
while time.monotonic() < deadline:
if process.poll() is not None:
if not _process_group_exists(pgid):
return
time.sleep(0.1)
try:
os.killpg(pgid, signal.SIGKILL)
except ProcessLookupError:
return
process.wait(timeout=timeout_s)
if process.poll() is None:
process.wait(timeout=timeout_s)
def run_trial(trial_spec_path: Path) -> dict[str, Any]: