You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mis-detects curio as asyncio when running in curio in asyncio.to_thread (py3.9) #20
changed the title [-]mis-detects asyncio when running in curio in asyncio.to_thread (py3.9)[/-][+]mis-detects curio as asyncio when running in curio in asyncio.to_thread (py3.9)[/+]on Mar 3, 2021
Calling curio.run inside an asyncio task and calling asyncio.run inside a curio task are probably not fixable? At least I can't think of any way to do it without some explicit assistance from curio+asyncio. I'm not super bothered about that though, because doing this doesn't really make any sense anyway.
The top-level problem with to_thread does seem like a real issue though. It's happening because sniffio uses a ContextVar to track the current loop, so (1) sniffio is detecting asyncio and caching that in the contextvar, (2) asyncio is blindly copying the entire Context into the thread, (3) sniffio in the thread is re-reading its cached contextvar, even though it's no longer appropriate.
As we discussed a bit on gitter, I'm no longer convinced that ContextVars are really the right approach here, for this and other reasons (in particular, the cvar approach originally assumed that trio/asyncio interop would be through trio-asyncio where we emulate the asyncio operations and have a chance to fix up the cvars; but now trio/asyncio via guest mode is also on the table, and I don't see how the cvar approach can work there, because it uses native asyncio ops and there's no chance for us to intercept trio<->asyncio transitions and fixup the cvar.)
Probably we should just use a thread-local instead of a cvar. It'll make sniffio slightly slower on non-cooperating libraries like asyncio/curio, but I don't see any way to avoid that. Something like:
Activity
[-]mis-detects asyncio when running in curio in asyncio.to_thread (py3.9)[/-][+]mis-detects curio as asyncio when running in curio in asyncio.to_thread (py3.9)[/+]graingert commentedon Mar 4, 2021
this also happens when running curio inside an asyncio coro
graingert commentedon Mar 4, 2021
and the other way around, running asyncio inside a curio coro:
njsmith commentedon Mar 4, 2021
Calling
curio.runinside an asyncio task and callingasyncio.runinside a curio task are probably not fixable? At least I can't think of any way to do it without some explicit assistance from curio+asyncio. I'm not super bothered about that though, because doing this doesn't really make any sense anyway.The top-level problem with
to_threaddoes seem like a real issue though. It's happening because sniffio uses aContextVarto track the current loop, so (1) sniffio is detecting asyncio and caching that in the contextvar, (2) asyncio is blindly copying the entireContextinto the thread, (3) sniffio in the thread is re-reading its cached contextvar, even though it's no longer appropriate.As we discussed a bit on gitter, I'm no longer convinced that
ContextVars are really the right approach here, for this and other reasons (in particular, the cvar approach originally assumed that trio/asyncio interop would be through trio-asyncio where we emulate the asyncio operations and have a chance to fix up the cvars; but now trio/asyncio via guest mode is also on the table, and I don't see how the cvar approach can work there, because it uses native asyncio ops and there's no chance for us to intercept trio<->asyncio transitions and fixup the cvar.)Probably we should just use a thread-local instead of a cvar. It'll make sniffio slightly slower on non-cooperating libraries like asyncio/curio, but I don't see any way to avoid that. Something like:
graingert commentedon Mar 4, 2021
You have to walk the stack and see which library is calling g.send
njsmith commentedon Mar 4, 2021
We're not walking the stack just to fix an issue with code that no-one should even be writing in the first place :-)
stop setting asyncio in the contextvar
stop setting asyncio in the contextvar
graingert commentedon Aug 1, 2022
Turns out it's totally fixable because asyncio and curio both communicate via sys.set_asyncgen_hooks