Closed ojii closed 2 years ago
Personally, I find ContextVar
to be a bad idea, and it's not clear when a context is created/removed. The purpose of ContexVar
appears to be more related to have a context for each request or similar, I think using them as an app-wide global will only lead to problems (problems I've recently encountered with another library that uses them like this).
Having said that, it does appear to be documented behaviour: https://docs.aiohttp.org/en/stable/web_advanced.html#contextvars-support So, I'm wondering if the bug might even be in aiohttp.
I believe that @asvetlov mentioned that there's something that needs to be improved wrt case (long ago, in private chats). But I don't remember what exactly and I ended up not going down the rabbit hole myself back then. I seem to recall some connection to the cleanup contexts but maybe I'm misremembering.
Should be done as a part of pytest-asyncio
plugin. Stay tuned.
@Dreamsorcerer contextvars were initially created to solve decimal localcontext problem when it is used in async code.
Generally speaking, they are as bad as thread-local variables, not better and not worse. I'm trying to avoid contextvars usage in architecture design, but they are useful for tooling like logging, distributed tracing, etc.
The following test passes but then errors in the finalizers when it tries to run the app shutdown, as the context is no longer available:
Error: