Repository navigation
fix(python): reap the managed server off the asyncio event loop - #3015
Conversation
`LLM.close()` and `AsyncLLM.close()` marked the client closed, closed the HTTP client and then stopped the managed server in straight-line code. An error from the HTTP close, or a cancellation while awaiting it, skipped the server shutdown, and the closed flag made every later `close()` return early, so the child kept running. Stop the server in a `finally` around the HTTP close in both clients. The original error or cancellation still propagates.
`AsyncLLM.close()` called the synchronous `ManagedServer.close()` on the event loop thread, so every other task stalled for as long as the child took to exit (terminate, wait, kill, log-thread join). Run that cleanup in the loop's default executor and await it. The wait is shielded: a cancellation that arrives during shutdown, including a second one, is re-raised only after the child has been reaped, so the cleanup is never left running behind a cancelled task. When there is no asyncio loop (another async backend) or the executor no longer accepts work, the cleanup runs synchronously as before. Errors from the HTTP close still propagate.
…e-yields # Conflicts: # python/src/mlxcel/_async_client.py
AsyncLLM.close() waited on the cleanup worker with asyncio.shield(). When the task had been cancelled during the wait and the worker then raised, the last shield await surfaced the worker's RuntimeError and the recorded CancelledError was dropped, so a cancelled close() finished with an ordinary exception and broke the caller's cancel() and timeout contracts. Wait with asyncio.wait(), which neither cancels the worker nor raises its error, read the outcome once, and re-raise a pending cancellation ahead of a worker error. A new cancel-worker-error case covers it; it fails on the shield version. Validation: pytest python/tests -m "not e2e" on Python 3.11 and 3.9 (75 passed), ruff check, ruff format --check, mypy python/src. Refs lablup#3014
|
Thank you for this one; the loop-driven SIGTERM handshake in the test is a nice way to make a blocking close fail deterministically. Review against #3014: the design holds up. One gap found and fixed in f2d554a: with I also merged |
Summary
AsyncLLM.close()stopped the managed server with a synchronous call on the event loop thread, freezing every other task until the server process had exited. The shutdown now runs in the loop's default executor and is awaited; cancellation is re-raised only after the server has been reaped.Related issues
Closes #3014.
Stacked on #3011 (the fix for #3010): the first commit of this branch is that PR, and only the second commit is new. Both change the same
finallyblock ofAsyncLLM.close().Type of change
fix— bug fixCause
AsyncLLM.close()(python/src/mlxcel/_async_client.py) calledself._server.close().ManagedServer.close()blocks inproc.wait(timeout=self._shutdown_grace), possibly a second wait afterkill(), andself._log_thread.join(timeout=1.0).Change
ManagedServer.closeis submitted to the default executor and awaited throughasyncio.wait, which neither cancels the worker nor raises its error. If the task is cancelled while waiting, once or repeatedly, the wait continues and theCancelledErroris raised after the worker has finished, so the cleanup is never left running behind a cancelled task and the child is always reaped beforeclose()returns.httpx), or when the executor no longer accepts work (it was shut down, or the interpreter is exiting), the cleanup runs synchronously as it did before.python/tests/test_async_shutdown.py(new, 15 cases), each with a real child process:test_async_close_yields_and_finishes_reaping(8 modes): the child's SIGTERM handler waits for a file that only the event loop creates, so aclose()that blocks the loop cannot finish gracefully. Modes: direct close, context manager, HTTP-close error, cancellation inside the HTTP close, one and two cancellations during the shutdown, a child that has to be killed, an error from the shutdown itself.test_async_connect_close_does_not_start_a_worker.test_close_reaps_when_the_executor_takes_no_work(4 cases): afterloop.shutdown_default_executor(), and with a submission that raises, each with and without an HTTP-close error.test_close_without_asyncio_preserves_cleanup_and_http_error(2 cases).Before / after
Linux aarch64, a child that takes 350 ms to exit on SIGTERM, a 10 ms heartbeat task, two runs each:
mainclose()/ largest gapclose()/ largest gapclose()close()takes as long as before; what changes is that the loop keeps running meanwhile.New test file on the parent commit and on this PR, in both environments (Python 3.9.25 with openai 1.40.0 / httpx 0.27.0, and Python 3.12.3 with openai 3.49.0 / httpx 0.28.1):
yields_and_finishes_reapingmodes), 7 passed-m "not e2e")Test plan
Run on this PR's source (the second commit of the branch; tree
6031da5bcf5a).python -m pytest -m "not e2e"inpython/: 74 passed, 2 deselected, on Python 3.9.25 with the declared lower bounds and on Python 3.12.3 with current releasesmain, the parent and this PR, both environmentsruff check python,ruff format --check python: pass;mypy python/src(Python 3.9 target): passclose()succeeds and the child is reaped); and, on the final source, aclose()called from a non-daemon thread's loop after the main thread had ended (child reaped, exit -15, on 3.9.25 and 3.12.3). The suite's no-asyncio and executor-refusal cases exercise the same branchesNotes for reviewers
CancelledErroronly after the child has exited, which is bounded by the existing grace period (twice, if the child has to be killed). The alternative, returning at once, would leave a worker thread and possibly a child process behind a client that already reports itself closed.main. This PR does not make shutdown non-blocking on Trio.asyncio.shield, so when the task was cancelled and the worker then raised, the worker's error replaced the pendingCancelledError. The wait now usesasyncio.wait, a pending cancellation wins over a worker error, and a ninth mode (cancel-worker-error) covers it. Suite after mergingmain: 75 passed, 2 deselected on Python 3.11 and 3.9 (macOS).Checklist
docs/: not needed// Used by: ...comments: not applicable (Python).envfiles committed