-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Regression: it should be possible to close async iterators multiple times #83787
Comments
In bpo-39386, the 'aclose' method for async generators was fixed so that the following broken code would correctly raise an error: # -- bad code --
async def agen_fn():
yield
async def do_bad_thing():
agen = agen_fn()
aclose_coro = agen.aclose()
await aclose_coro
# Should raise RuntimeError:
await aclose_coro
asyncio.run(do_bad_thing()) # ----------------- However, the merged fix also broke the following correct code, which should *not* raise an error: # -- good code --
async def agen_fn():
yield
async def do_good_thing():
agen = agen_fn()
await agen.aclose()
# Should *not* raise an error, but currently does in Python dev branches:
await agen.aclose()
asyncio.run(do_good_thing()) # ---------------- It's not supported to iterate the same coroutine object twice. However, making two *independent* calls to aclose should return two independent coroutine objects, and it should be legal to iterate over each object once. This can also occur even if there's only a single call to 'aclose'. For example, this is the recommended idiom for making sure that an async generator correctly closes all its resources: # -- good code --
async def agen_fn():
yield
async def careful_loop():
agen = agen_fn()
try:
async for _ in agen:
pass
finally:
await agen.aclose()
asyncio.run(careful_loop()) # ------------------- On released Python, the code above runs correctly. On the latest Python dev branches, it raises a RuntimeError. So basically the problem is that the fix for bpo-39386 is storing the "was aclose iterated?" state on the async generator object, but in fact it needs to be stored on the aclose coroutine object. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: