python-trio / trio-asyncio

a re-implementation of the asyncio mainloop on top of Trio
Other
187 stars 37 forks source link

sniffio reports wrong current loop when used with trio_asyncio and trio 0.23 #123

Closed DaGenix closed 8 months ago

DaGenix commented 8 months ago

The following code works with Trio 0.22.2. However, it fails with 0.23.1:

import asyncio
import sniffio
import trio
from trio_asyncio import open_loop, aio_as_trio

@aio_as_trio
async def aio_func() -> None:
    await asyncio.sleep(1)  # The next line will fail. But, this sleep works fine which shows we are actually in asyncio context
    assert sniffio.current_async_library() == "asyncio"

async def main() -> None:
    assert sniffio.current_async_library() == "trio"
    async with open_loop():
        await aio_func()

if __name__ == "__main__":
    trio.run(main)
A5rocks commented 8 months ago

I'm going to guess this is because https://github.com/python-trio/trio/pull/2700

I haven't checked to see if your sample works before then, but cc @oremanj just in case!


OK so failure bisects to that -- it looks like trio-asyncio uses current_async_library_cvar which unfortunately does not take precedence over the new thread_local mechanism.

A5rocks commented 8 months ago

OK I'm pretty sure this is a trio-asyncio issue so transferring this over to there.