-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
docs: Add exception handling guidance for background tasks #11870
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: master
Are you sure you want to change the base?
Conversation
for more information, see https://pre-commit.ci
|
Added news fragment and contributor entry. |
CodSpeed Performance ReportMerging #11870 will not alter performanceComparing Summary
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #11870 +/- ##
=======================================
Coverage 98.74% 98.75%
=======================================
Files 127 127
Lines 44172 44172
Branches 2342 2342
=======================================
+ Hits 43619 43620 +1
Misses 392 392
+ Partials 161 160 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
|
As part of the client docs, this doesn't seem like it has anything to do with aiohttp? This feels like general asyncio knowledge and should appear in the asyncio.create_task() docs. For the server docs, we already have: |
|
Thanks for the feedback — I agree that the underlying semantics are defined by asyncio, not aiohttp. The motivation for placing this in the aiohttp client documentation is not to re-document asyncio.create_task(), but to address a specific pattern that commonly leads to issues in aiohttp client code. In practice, it's common to see background HTTP requests spawned via asyncio.create_task() to decouple request latency from control flow (e.g., prefetching, warm-up calls, telemetry). When this pattern is combined with aiohttp requests, exceptions raised during the request lifecycle (ClientConnectorError, ClientResponseError, timeouts, etc.) can become invisible to application logic unless explicitly handled. While this behavior is documented at the asyncio level, many aiohttp users encounter it through aiohttp usage patterns, not through generic asyncio examples. The intent here is to add a narrowly scoped warning at the point where aiohttp users are most likely to adopt this pattern. That said, I'm happy to:
Please let me know what approach would fit best with the docs structure. |
This still feels like it could apply to anything they are using, so I'm not really convinced. Your second example also creates a task and then immediately awaits on it, which entirely defeats the point. .add_done_callback() is also not something I think we should be recommending; I'd instead point users towards aiojobs until a similar thing arrives in asyncio itself. @webknjaz @bdraco Do you think this is something that should be included in our docs? |
|
Thanks for the detailed feedback — this is helpful. I agree on a couple of points:
The intent here isn't to recommend create_task() patterns broadly, but to address a footgun that aiohttp client users frequently run into when doing background HTTP work (telemetry, warm-up calls, best-effort side requests), where aiohttp-specific exceptions (ClientConnectorError, timeouts, response errors) end up being silently lost. That said, I think your suggestion about aiojobs is a much better direction and makes this genuinely aiohttp-scoped rather than generic asyncio guidance. I'm happy to revise this PR to:
Something along the lines of:
Let me know if this direction works—I'm happy to revise accordingly, or if you'd still prefer I close this given the scope concerns, I completely understand. |
What do these changes do?
Adds documentation and runnable examples explaining how to handle exceptions in background tasks created with asyncio.create_task().
The changes address a common pitfall where developers use fire-and-forget task patterns with aiohttp requests, unaware that exceptions raised in background tasks do not propagate to the spawning context. This can lead to silent failures in production systems.
Are there changes in behavior for the user?
No behavioral changes to the library. This is documentation-only.
Users will now find:
Is it a substantial burden for the maintainers to support this?
No. This is purely additive documentation with no code changes.
Related issue number
No existing issue. This addresses a documentation gap identified through production experience.
Checklist
CONTRIBUTORS.txtCHANGES/folder