-
Notifications
You must be signed in to change notification settings - Fork 671
fix(async_engine): make safe_run cancellation cleanup reliable with shield and SafeRunException #4439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(async_engine): make safe_run cancellation cleanup reliable with shield and SafeRunException #4439
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -269,9 +269,26 @@ async def safe_run(self, handle, session, **kwargs): | |
| yield generator | ||
| except (Exception, asyncio.CancelledError, GeneratorExit) as e: # noqa | ||
| logger.exception(f'[safe_run] session {session.session_id} exception caught: {e}') | ||
| await session.async_abort() | ||
| # Use asyncio.shield to protect cleanup coroutines from being cancelled. | ||
| # When a task is in cancelling state, bare `await` raises CancelledError | ||
| # immediately. shield ensures the inner coroutine runs to completion. | ||
| # The outer `except (asyncio.CancelledError, Exception)` catches the | ||
| # CancelledError that shield itself re-raises at the await point. | ||
| try: | ||
| await asyncio.shield(handle.async_cancel(session.session_id)) | ||
| except (asyncio.CancelledError, Exception) as cancel_e: | ||
| logger.debug(f'[safe_run] session {session.session_id} async_cancel exception caught: {cancel_e}') | ||
| if self.backend == 'pytorch': | ||
| await handle.async_end(session.session_id) | ||
| logger.info(f'[safe_run] session {session.session_id} ending session') | ||
| try: | ||
| await asyncio.shield(handle.async_end(session.session_id)) | ||
| except (asyncio.CancelledError, Exception) as end_e: | ||
| logger.debug(f'[safe_run] session {session.session_id} async_end exception caught: {end_e}') | ||
|
Comment on lines
+277
to
+286
|
||
| # Wrap as SafeRunException so that the outer `request_handle` context | ||
| # manager in `session_manager.py` can distinguish a handled cancellation (caught by | ||
| # `except SafeRunException: pass`) from an unexpected CancelledError. | ||
| # Without this, the suppressed exception leaves the task in cancelling | ||
| # state, causing a second CancelledError at the next await point. | ||
| raise SafeRunException(f'Safe run exception for session {session.session_id}') from e | ||
| finally: | ||
| await generator.aclose() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The explanatory comment around
asyncio.shieldis misleading:shieldprevents the inner awaitable from being cancelled, but it does not ensure the outer task waits for it to finish once the outer task is cancelled (it can still raiseCancelledErrorimmediately). Please adjust the comment to match actualasyncio.shieldbehavior so future changes don’t rely on an incorrect guarantee.