← index #5795PR #6664
Off-topic · high · value 0.089
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 · PULL REQUEST

extmod/uasyncio: Delay calling Loop.call_exception_handler by 1 loop, and add Task.done() method

mergedby dpgeorgeopened 2020-11-30updated 2020-12-02
extmod

When a tasks raises an exception which is uncaught, and no other task await's on that task, then an error message is printed (or a user function called) via a call to Loop.call_exception_handler. In CPython this call is made when the Task object is freed (eg via reference counting) because it's at that point that it is known that the exception that was raised will never be handled.

MicroPython does not have reference counting and the current behaviour is to deal with uncaught exceptions as early as possible, ie as soon as they terminate the task. But this can be undesirable because in certain cases a task can start and raise an exception immediately (before any await is executed in that task's coro) and before any other task gets a chance to await on it to catch the exception.

This commit changes the behaviour so that tasks which end due to an uncaught exception are scheduled one more time for execution, and if they are not await'ed on by the next scheduling loop, then the exception handler is called (eg the exception is printed out).

As part of this, task.coro==None is no longer the way to detect if a task is finished, so the Task.done() method is added.

IMO this fix is useful in its own right, but it's also a precursor to fix wait_for() cancellation in a clean way (see also #5931).

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