-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
Contextvars + async generator + break + finally #118944
Comments
Yes this is expected, because the async generator finalization is triggered by the garbage collector, where asyncio uses sys.set_asyncgen_hooks() to aclose the async generator in a new task. See https://peps.python.org/pep-0525/#finalization If you want to ensure finalization in the same context use async def main():
async with contextlib.aclosing(gen()) as agen:
async for i in agen:
print(i)
break() |
See also https://docs.python.org/3/reference/expressions.html#asynchronous-generator-functions
And https://docs.python.org/3/library/contextlib.html#contextlib.aclosing
|
I'm going to close this, as working as documented |
Bug report
Bug description:
result :
In finally block it's not possible to manually reset the contextvars
CPython versions tested on:
3.12
Operating systems tested on:
macOS
The text was updated successfully, but these errors were encountered: