Skip to content

[Serve] Clean up HAProxy subprocess when startup readiness is cancelled - #68653

Open
banlor wants to merge 1 commit into
ray-project:masterfrom
banlor:fix/serve-haproxy-cancel-orphan
Open

banlor wants to merge 1 commit into
ray-project:masterfrom
banlor:fix/serve-haproxy-cancel-orphan

Conversation

@banlor

@banlor banlor commented Nov 29, 2026

Copy link
Copy Markdown

Description

HAProxyApi._start_and_wait_for_haproxy() caught only Exception around await self._wait_for_hap_availability(...). Because asyncio.CancelledError inherits from BaseException, cancelling a start or reload task while waiting for readiness skipped proc.kill(), await proc.wait(), and self._retire_log_files(proc), leaving an orphaned HAProxy process running.

This catches BaseException instead, ignores ProcessLookupError if the child exited right before kill(), finishes await proc.wait() even if another cancellation arrives during cleanup, retires the process log files, and re-raises.

Added unit tests in python/ray/serve/tests/unit/test_haproxy_process_manager.py for real subprocess cancellation/error cleanup, ProcessLookupError, and cancellation during proc.wait().

Related issues

Fixes #68637

Additional information

Ran python/ray/serve/tests/unit/test_haproxy*.py (26 passed, 4 Linux /proc tests skipped on macOS) and ruff check on both modified files.

@banlor
banlor requested a review from a team as a code owner November 29, 2026 16:01

@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 improves subprocess cleanup and log retirement in the HAProxy process manager when startup fails or is cancelled. It changes the exception handling in _start_and_wait_for_haproxy to catch BaseException and defers cancellation until the child process is reaped and logs are retired. It also adds comprehensive unit tests to cover these scenarios. The reviewer pointed out a critical issue where the proposed cancellation handling could lead to an infinite loop on Python 3.11+ due to task cancellation behavior, and provided a backward-compatible code suggestion using uncancel() to resolve it.

Comment on lines +1251 to 1267
except BaseException:
# Until readiness succeeds, the caller has no handle to this child.
cancelled = None
if proc.returncode is None:
proc.kill()
await proc.wait()
# The proc has exited; retire its log files so a failed start/reload
# doesn't orphan them outside the bounded ring.
with suppress(ProcessLookupError): # May have exited before kill().
proc.kill()
while True:
try:
await proc.wait()
break
except asyncio.CancelledError as exc:
# Defer cancellation until the child is reaped and logs retired.
cancelled = exc
self._retire_log_files(proc)
if cancelled is not None:
raise cancelled
raise

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.

high

In Python 3.11+, catching asyncio.CancelledError and then performing another await (such as await proc.wait()) will immediately raise asyncio.CancelledError again unless the task's cancellation is explicitly cleared using task.uncancel().

Because of this, the while True: loop on lines 1257-1263 will result in an infinite loop on Python 3.11+ when the task is cancelled, spinning the CPU at 100% and preventing the process from being reaped.

To fix this in a backward-compatible way (supporting Python 3.8 to 3.12+), we can check if the current task has an uncancel method and call it before awaiting proc.wait().

Suggested change
except BaseException:
# Until readiness succeeds, the caller has no handle to this child.
cancelled = None
if proc.returncode is None:
proc.kill()
await proc.wait()
# The proc has exited; retire its log files so a failed start/reload
# doesn't orphan them outside the bounded ring.
with suppress(ProcessLookupError): # May have exited before kill().
proc.kill()
while True:
try:
await proc.wait()
break
except asyncio.CancelledError as exc:
# Defer cancellation until the child is reaped and logs retired.
cancelled = exc
self._retire_log_files(proc)
if cancelled is not None:
raise cancelled
raise
except BaseException as exc:
# Until readiness succeeds, the caller has no handle to this child.
cancelled = exc if isinstance(exc, asyncio.CancelledError) else None
if proc.returncode is None:
with suppress(ProcessLookupError): # May have exited before kill().
proc.kill()
if cancelled is not None:
current_task = asyncio.current_task()
if current_task and hasattr(current_task, 'uncancel'):
current_task.uncancel()
try:
await proc.wait()
except asyncio.CancelledError as wait_exc:
cancelled = wait_exc
self._retire_log_files(proc)
if cancelled is not None:
raise cancelled
raise

This branch has not been deployed

No deployments
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.

[Bug] Ray Serve HAProxyApi._start_and_wait_for_haproxy orphans live HAProxy subprocess on asyncio.CancelledError

1 participant