Don't re-await main_task in run_app finally when it's already done#12493
Open
AndrewKarelin wants to merge 3 commits intoaio-libs:masterfrom
Open
Don't re-await main_task in run_app finally when it's already done#12493AndrewKarelin wants to merge 3 commits intoaio-libs:masterfrom
AndrewKarelin wants to merge 3 commits intoaio-libs:masterfrom
Conversation
The cancel/re-await branch in run_app's finally is meant for the GracefulExit / KeyboardInterrupt path, where main_task is still running. On the startup-failure path, main_task is already done with the exception, and re-running loop.run_until_complete(main_task) calls Future.result(), which executes ``raise self._exception.with_traceback(self._exception_tb)``. That with_traceback call resets exc.__traceback__ to the saved _exception_tb, which by this point reflects only the outermost frames. The deep frames (cleanup_ctx / on_startup body, the user code that raised) are lost before the exception reaches the caller. Skip the second run_until_complete when main_task is already done. This preserves the full traceback in the startup-failure case and is a no-op for the graceful-shutdown path. Regression since 3.10.6.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #12493 +/- ##
=======================================
Coverage 98.92% 98.92%
=======================================
Files 134 134
Lines 46750 46761 +11
Branches 2429 2430 +1
=======================================
+ Hits 46248 46259 +11
Misses 373 373
Partials 129 129
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Merging this PR will not alter performance
Comparing Footnotes
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
In
web.run_app'sfinallyblock, only callmain_task.cancel()and re-runloop.run_until_complete(main_task)when the task is still running.finally: try: - main_task.cancel() - with suppress(asyncio.CancelledError): - loop.run_until_complete(main_task) + if not main_task.done(): + main_task.cancel() + with suppress(asyncio.CancelledError): + loop.run_until_complete(main_task) finally: _cancel_tasks(asyncio.all_tasks(loop), loop) loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()Why
The cancel/re-await branch is intended for the
GracefulExit/KeyboardInterruptpath, where the firstrun_until_completereturned without drainingmain_task. On the startup-failure path,main_taskis alreadydone()with the exception, and re-running it strips most of the traceback before it reaches the caller.Future.result()on an already-finished task doesraise self._exception.with_traceback(self._exception_tb). Thewith_tracebackcall resetsexc.__traceback__to the earlier-saved_exception_tb, which by this point reflects only the outermost frames. The deep frames the user actually needs (thecleanup_ctx/on_startupbody, the user code that raised) are gone.Skipping the second
run_until_completewhen the task is already done preserves the full traceback in the startup-failure case and is a no-op in the graceful-shutdown case (the task is still running there).Versions affected
masterReproducer
Before this PR (3.10.6..3.13.5): 3 frames, no
bad_ctxshown. Traceback stops atloop.run_until_complete.After this PR: 9 frames, including:
The same chain you get today via
asyncio.run(runner.setup())— the bug is strictly inweb.run_app'sfinally.Test
Added
test_run_app_preserves_startup_tracebackintests/test_run_app.py: registers a failingcleanup_ctx, asserts that the user function's frame appears in the traceback walked from the propagated exception.Compatibility
GracefulExit/KeyboardInterruptpath: identical behavior (task is not done at that point, so the inner block runs as before).loop.run_until_complete.main_taskis done, secondrun_until_completeis skipped — same observable behavior, slightly cheaper.CHANGES file will be added once the PR number is assigned.