micropython/extmod
Damien George 514bf1a191 extmod/uasyncio: Fix race with cancelled task waiting on finished task.
This commit fixes a problem with a race between cancellation of task A and
completion of task B, when A waits on B.  If task B completes just before
task A is cancelled then the cancellation of A does not work.  Instead,
the CancelledError meant to cancel A gets passed through to B (that's
expected behaviour) but B handles it as a "Task exception wasn't retrieved"
scenario, printing out such a message (this is because finished tasks point
their "coro" attribute to themselves to indicate they are done, and
implement the throw() method, but that method inadvertently catches the
CancelledError).  The correct behaviour is for B to bounce that
CancelledError back out.

This bug is mainly seen when wait_for() is used, and in that context the
symptoms are:
- occurs when using wait_for(T, S), if the task T being waited on finishes
  at exactly the same time as the wait-for timeout S expires
- task T will have run to completion
- the "Task exception wasn't retrieved message" is printed with
  "<class 'CancelledError'>" as the error (ie no traceback)
- the wait_for(T, S) call never returns (it's never put back on the
  uasyncio run queue) and all tasks waiting on this are blocked forever
  from running
- uasyncio otherwise continues to function and other tasks continue to be
  scheduled as normal

The fix here reworks the "waiting" attribute of Task to be called "state"
and uses it to indicate whether a task is: running and not awaited on,
running and awaited on, finished and not awaited on, or finished and
awaited on.  This means the task does not need to point "coro" to itself to
indicate finished, and also allows removal of the throw() method.

A benefit of this is that "Task exception wasn't retrieved" messages can go
back to being able to print the name of the coroutine function.

Fixes issue #7386.

Signed-off-by: Damien George <damien@micropython.org>
2021-06-16 13:02:37 +10:00
..
axtls-include py/py.mk: Build axtls library directly from its source files. 2018-09-08 00:07:23 +10:00
btstack extmod/modbluetooth: Separate enabling of "client" from "central". 2021-02-19 17:53:43 +11:00
crypto-algorithms extmod/crypto-algorithms: Add source to header and populate copyright. 2019-08-22 17:20:16 +10:00
lwip-include all: Unify header guard usage. 2017-07-18 11:57:39 +10:00
nimble extmod/modbluetooth: Separate enabling of "client" from "central". 2021-02-19 17:53:43 +11:00
re1.5 extmod/re1.5: Check and report byte overflow errors in _compilecode. 2021-04-06 13:36:42 +10:00
uasyncio extmod/uasyncio: Fix race with cancelled task waiting on finished task. 2021-06-16 13:02:37 +10:00
uzlib extmod/uzlib: Explicitly cast ptr-diff-expr to unsigned. 2019-12-23 00:07:03 +11:00
webrepl all: Rename "sys" module to "usys". 2020-09-04 00:10:24 +10:00
extmod.cmake extmod/extmod.cmake: Add support to build btree module with CMake. 2021-04-09 13:33:26 +10:00
extmod.mk extmod: Disable -Wmissing-field-initializers for lfs2. 2020-10-22 11:47:36 +02:00
machine_i2c.c extmod/machine_i2c: Add init protocol method for generic I2C bindings. 2020-11-23 19:45:04 +11:00
machine_i2c.h extmod/machine_i2c: Add init protocol method for generic I2C bindings. 2020-11-23 19:45:04 +11:00
machine_mem.c extmod/machine_mem: Only allow integers in machine.memX subscript. 2020-11-13 11:13:37 +11:00
machine_mem.h all: Use the name MicroPython consistently in comments 2017-07-31 18:35:40 +10:00
machine_pinbase.c extmod/machine_pinbase: Put PinBase singleton in ROM. 2017-09-12 16:00:21 +10:00
machine_pinbase.h all: Unify header guard usage. 2017-07-18 11:57:39 +10:00
machine_pulse.c esp8266: Provide custom machine_time_pulse_us that feeds soft WDT. 2019-07-01 22:53:00 +10:00
machine_pulse.h all: Unify header guard usage. 2017-07-18 11:57:39 +10:00
machine_signal.c all: Reformat C and Python source code with tools/codeformat.py. 2020-02-28 10:33:03 +11:00
machine_signal.h all: Unify header guard usage. 2017-07-18 11:57:39 +10:00
machine_spi.c extmod/machine_spi: Remove "id" arg in SoftSPI constructor. 2020-10-01 12:57:10 +10:00
machine_spi.h ports: Support legacy soft I2C/SPI construction via id=-1 arg. 2020-10-01 12:57:10 +10:00
misc.h extmod/uos_dupterm: Add mp_uos_dupterm_poll to poll all dupterms. 2019-07-01 17:10:12 +10:00
modbluetooth.c extmod/modbluetooth: Free temp arrays in gatts register services. 2021-03-16 13:55:45 +11:00
modbluetooth.h extmod/modbluetooth: Separate enabling of "client" from "central". 2021-02-19 17:53:43 +11:00
modbtree.c extmod/modbtree: Retain reference to underlying stream so it's not GC'd. 2020-05-02 16:08:04 +10:00
modframebuf.c extmod/modframebuf: Change int to unsigned int in format methods args. 2021-01-29 23:57:10 +11:00
modlwip.c extmod/modlwip: Fix error return for TCP recv when not connected. 2020-08-30 13:20:51 +10:00
modonewire.c extmod/modonewire: Use pin_od_high/pin_od_low instead of pin_write. 2021-01-29 23:57:10 +11:00
moduasyncio.c extmod/uasyncio: Fix race with cancelled task waiting on finished task. 2021-06-16 13:02:37 +10:00
modubinascii.c extmod/modubinascii: Update code, docs for hexlify now CPython has sep. 2020-12-14 14:35:29 +11:00
moducryptolib.c all: Format code to add space after C++-style comment start. 2020-04-23 11:24:25 +10:00
moductypes.c extmod/moductypes: Fix size and offset calculation for ARRAY of FLOAT32. 2021-05-06 13:11:33 +10:00
moduhashlib.c extmod/moduhashlib: Put hash obj in final state after digest is called. 2021-05-26 21:44:46 +10:00
moduheapq.c all: Format code to add space after C++-style comment start. 2020-04-23 11:24:25 +10:00
modujson.c all: Format code to add space after C++-style comment start. 2020-04-23 11:24:25 +10:00
modurandom.c extmod/modurandom: Support an argument of bits=0 to getrandbits. 2021-05-30 17:05:56 +10:00
modure.c extmod/modure: Allow \\ in re.sub replacements. 2020-09-30 23:18:34 +10:00
moduselect.c extmod: Remove old comments used for auto-doc generation. 2021-05-06 12:27:31 +10:00
modussl_axtls.c extmod/modussl: Fix ussl read/recv/send/write errors when non-blocking. 2021-02-17 11:50:54 +11:00
modussl_mbedtls.c extmod/modussl: Fix ussl read/recv/send/write errors when non-blocking. 2021-02-17 11:50:54 +11:00
modutimeq.c all: Format code to add space after C++-style comment start. 2020-04-23 11:24:25 +10:00
moduwebsocket.c all: Format code to add space after C++-style comment start. 2020-04-23 11:24:25 +10:00
moduwebsocket.h extmod/moduwebsocket: Refactor `websocket` to `uwebsocket`. 2019-02-14 00:35:45 +11:00
moduzlib.c all: Use MP_ERROR_TEXT for all error messages. 2020-04-05 15:02:06 +10:00
modwebrepl.c all: Format code to add space after C++-style comment start. 2020-04-23 11:24:25 +10:00
mpbthci.c extmod/modbluetooth: Refactor stack/hci/driver/port bindings. 2020-09-08 11:41:31 +10:00
mpbthci.h extmod/modbluetooth: Refactor stack/hci/driver/port bindings. 2020-09-08 11:41:31 +10:00
network_cyw43.c all: Format code to add space after C++-style comment start. 2020-04-23 11:24:25 +10:00
network_cyw43.h extmod: Add network-level class binding to cyw43 driver. 2019-06-03 16:47:35 +10:00
uos_dupterm.c all: Rename mp_keyboard_interrupt to mp_sched_keyboard_interrupt. 2021-04-30 15:13:43 +10:00
utime_mphal.c extmod/utime_mphal: Add generic utime.time_ns() function. 2020-10-01 14:20:42 +10:00
utime_mphal.h extmod/utime_mphal: Add generic utime.time_ns() function. 2020-10-01 14:20:42 +10:00
vfs.c extmod/vfs: Check block 0 and 1 when auto-detecting littlefs. 2021-01-29 15:02:55 +11:00
vfs.h extmod/vfs: Factor out vfs mount-and-chdir helper from stm32. 2020-03-11 14:24:26 +11:00
vfs_blockdev.c all: Reformat C and Python source code with tools/codeformat.py. 2020-02-28 10:33:03 +11:00
vfs_fat.c extmod: Remove old comments used for auto-doc generation. 2021-05-06 12:27:31 +10:00
vfs_fat.h extmod: Factor out block-device struct to make independent of fatfs. 2019-10-29 12:12:37 +11:00
vfs_fat_diskio.c tools/uncrustify: Enable more opts to remove space between func and '('. 2020-06-19 22:07:32 +10:00
vfs_fat_file.c all: Reformat C and Python source code with tools/codeformat.py. 2020-02-28 10:33:03 +11:00
vfs_lfs.c py, extmod: Add explicit initializers for default values. 2020-10-22 11:47:36 +02:00
vfs_lfs.h extmod: Add VFS littlefs bindings. 2019-10-29 14:17:29 +11:00
vfs_lfsx.c extmod/vfs_lfs: Support mounting LFS filesystems in read-only mode. 2020-10-29 11:43:52 +11:00
vfs_lfsx_file.c extmod/vfs_lfs: Add mtime support to littlefs files. 2020-08-25 17:35:19 +10:00
vfs_posix.c extmod/vfs: Support larger integer range in VFS stat time fields. 2020-09-01 12:36:28 +10:00
vfs_posix.h extmod/vfs: Introduce a C-level VFS protocol, with fast import_stat. 2018-06-06 14:33:42 +10:00
vfs_posix_file.c extmod/vfs_posix_file: Allow closing an already closed file. 2021-02-11 22:54:41 +11:00
vfs_reader.c extmod/vfs_reader: Fix mp_reader_new_file to open file in "rb" mode. 2020-08-12 23:40:50 +10:00
virtpin.c all: Reformat C and Python source code with tools/codeformat.py. 2020-02-28 10:33:03 +11:00
virtpin.h all: Unify header guard usage. 2017-07-18 11:57:39 +10:00