extmod/uasyncio: Fix bug with task ending just after gather is cancel'd.

This fixes a bug where the gather is cancelled externally and then one of
its sub-tasks (that the gather was waiting on) finishes right between the
cancellation being queued and being executed.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2022-04-21 13:19:34 +10:00
parent f7454f850f
commit 28e7e15c0a
3 changed files with 33 additions and 4 deletions

View File

@@ -61,9 +61,13 @@ class _Remove:
async def gather(*aws, return_exceptions=False):
def done(t, er):
# Sub-task "t" has finished, with exception "er".
nonlocal state
if type(state) is not int:
# A sub-task already raised an exception, so do nothing.
if gather_task.data is not _Remove:
# The main gather task has already been scheduled, so do nothing.
# This happens if another sub-task already raised an exception and
# woke the main gather task (via this done function), or if the main
# gather task was cancelled externally.
return
elif not return_exceptions and not isinstance(er, StopIteration):
# A sub-task raised an exception, indicate that to the gather task.