[New-bugs-announce] [issue31960] Protection against using a Future with another loop only works with await

Antoine Pitrou report at bugs.python.org
Mon Nov 6 12:24:09 EST 2017


New submission from Antoine Pitrou <pitrou at free.fr>:

If you await a Future with another loop than a loop at instance creation (this is trivial to do accidentally when using threads), asyncio raises a RuntimeError.  But if you use another part of the Future API, such as add_done_callback(), the situation isn't detected.

For example, this snippet will run indefinitely:

import asyncio
import threading


fut = asyncio.Future()

async def coro(loop):
    fut.add_done_callback(lambda _: loop.stop())
    loop.call_later(1, fut.set_result, None)
    while True:
        await asyncio.sleep(100000)

def run():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(coro(loop))
    loop.close()


t = threading.Thread(target=run)
t.start()
t.join()

----------
components: asyncio
messages: 305662
nosy: giampaolo.rodola, haypo, pitrou, yselivanov
priority: normal
severity: normal
status: open
title: Protection against using a Future with another loop only works with await
type: behavior
versions: Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31960>
_______________________________________


More information about the New-bugs-announce mailing list