Repository navigation
Conversation
Signed-off-by: Mikhail Basov <[email protected]>
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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().
| 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 |
Description
HAProxyApi._start_and_wait_for_haproxy()caught onlyExceptionaroundawait self._wait_for_hap_availability(...). Becauseasyncio.CancelledErrorinherits fromBaseException, cancelling a start or reload task while waiting for readiness skippedproc.kill(),await proc.wait(), andself._retire_log_files(proc), leaving an orphaned HAProxy process running.This catches
BaseExceptioninstead, ignoresProcessLookupErrorif the child exited right beforekill(), finishesawait 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.pyfor real subprocess cancellation/error cleanup,ProcessLookupError, and cancellation duringproc.wait().Related issues
Fixes #68637
Additional information
Ran
python/ray/serve/tests/unit/test_haproxy*.py(26 passed, 4 Linux/proctests skipped on macOS) andruff checkon both modified files.