← index #5795Issue #6893
Related · high · value 0.222
QUERY · ISSUE

extmod/uasyncio Event.set() not safe in ISR

openby kevinkk525opened 2020-03-25updated 2020-04-07
extmod

If you call Event.set() in an ISR, you could end up losing all tasks that were awaiting the event (if the ISR is executed while uasyncio is sorting tasks on the queue e.g.).

I made a testscript a while ago to demonstrate this: https://gist.github.com/kevinkk525/5b89395604da3df3be7a015abcba04fa

CANDIDATE · ISSUE

uasyncio Event unexpected behaviour with only one task

closedby peterhinchopened 2021-02-13updated 2024-09-13

[EDIT] Apologies, @kevinkk525 has pointed out that this is a duplcate of https://github.com/micropython/micropython/issues/5843

This prints "start" then runs to completion where I would expect it to wait indefinitely on the Event:

import uasyncio as asyncio

evt = asyncio.Event()

async def main_loop():
    while True:
        print("start")
        await evt.wait()
        await asyncio.sleep(1)
        print("Got event")

asyncio.run(main_loop())

If an additional task is running the main_loop task behaves as expected, waiting forever:

import uasyncio as asyncio

evt = asyncio.Event()

async def main_loop():
    while True:
        print("start")
        await evt.wait()
        await asyncio.sleep(1)
        print("Got event")

async def main():
    asyncio.create_task(main_loop())
    while True:
        await asyncio.sleep(1)

asyncio.run(main())

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied