python-trio / trio-asyncio

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

Deadlock on KeyboardInterrupt in asyncio context #88

Open ghost opened 4 years ago

ghost commented 4 years ago

The following program raises a KeyboardInterrupt from an asyncio context. It prints

MARK
MARK
MARK
raising

and then hangs. The use of the nursery here is just to demonstrate that the mark function isn't being run anymore. I would expect the program to die with a KeyboardInterrupt Error, but it just hangs and pressing Control-C also doesn't stop the program.

import asyncio

import trio_asyncio
import trio

async def mark():
    while 1:
        print("MARK")
        await trio.sleep(1.0)

async def start():
    async def ctrlc_me():
        await asyncio.sleep(2.5)
        print("raising")
        raise KeyboardInterrupt("ctrl-c")

    async with trio.open_nursery() as nursery:
        nursery.start_soon(mark)
        try:
            await trio_asyncio.aio_as_trio(ctrlc_me)
        finally:
            print("DONE")

if __name__ == "__main__":
    trio_asyncio.run(start)