Uh oh!
There was an error while loading. Please reload this page.
Ensured explicit closing of async generators - #3593
Conversation
agronholm
commented
Jun 26, 2025
Note that this depends on another PR: #3592. I will mark as ready to review once that one has been merged. |
# Conflicts: # CHANGELOG.md
agronholm
commented
Jul 2, 2025
I checked locally with Python 3.10 that the supposedly missing coverage cannot be reproduced. I'm seeing 100% coverage, even without the |
agronholm
commented
Jul 2, 2025
Never mind, I was on the wrong branch. Fixing. |
agronholm
commented
Jul 2, 2025
@Kludex All good now. |
| raise StreamClosed() | ||
| async def __aiter__(self) -> AsyncIterator[bytes]: | ||
| def __aiter__(self) -> NoReturn: |
There was a problem hiding this comment.
It's not really necessary here. __aiter__() can return any arbitrary object that supports the AsyncIterator protocol, and the implementation here just raises NotImplementedError.
There was a problem hiding this comment.
@tomchristie this is not an important part of this PR so if you object, I can just revert it.
There was a problem hiding this comment.
On another note, is either one of you at EuroPython?
There was a problem hiding this comment.
On another note, is either one of you at EuroPython?
I'm not. 😞
| @asynccontextmanager | ||
| async def safe_async_iterate( |
There was a problem hiding this comment.
Interesting. I'm curious about this vs. aclosing.
https://docs.python.org/3/library/contextlib.html#contextlib.aclosing
There was a problem hiding this comment.
I originally went with aclosing() but it doesn't work very well with arbitrary async iterables.
| ### Fixed | ||
| * Explicitly close all async generators to ensure predictable behavior |
lovelydinosaur
commented
Sep 16, 2025
Thanks for your time on this @agronholm. The 1.0 pre-release deals with this comprehensively. There's no I/O iteration or generators, just regular |
ofek
commented
Sep 17, 2025
I haven't been following the developments. Are the changes you mentioned due to performance? |
agronholm
commented
Sep 17, 2025
No, but correctness. Async generators are supposed to be explicitly closed when you're done with them. But this PR is obsolete as httpx is transitioning to a new API in which I'm told this is a non-issue anyway. |
ofek
commented
Sep 17, 2025
My apologies for not being explicit, I was referring to the removal of generators/the new design. I'm always interested in hearing about architectural changes to this library because at work we have a task I created a while back to deprecate our use of |
Summary
This change ensures that async generators are always explicitly closed, even if the task running the generator is cancelled or another exception is raised. This prevents unpredictable behavior and also avoids the asyncgen finalizer warnings on Trio.
Checklist