extmod/uasyncio: Truncate negative sleeps to 0.

Otherwise a task that continuously awaits on a large negative sleep can
monopolise the scheduler (because its wake time is always less than
everything else in the pairing heap).

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2020-08-20 23:13:25 +10:00
parent 20948a3d54
commit 55c76eaac1
3 changed files with 3 additions and 1 deletions

View File

@ -53,7 +53,7 @@ class SingletonGenerator:
# Use a SingletonGenerator to do it without allocating on the heap
def sleep_ms(t, sgen=SingletonGenerator()):
assert sgen.state is None
sgen.state = ticks_add(ticks(), t)
sgen.state = ticks_add(ticks(), max(0, t))
return sgen

View File

@ -22,6 +22,7 @@ async def main():
t1 = asyncio.create_task(task(1, -0.01))
t2 = asyncio.create_task(task(2, 0.1))
t3 = asyncio.create_task(task(3, 0.2))
t3 = asyncio.create_task(task(4, -100))
await asyncio.sleep(0.5)
t1.cancel()
t2.cancel()

View File

@ -3,6 +3,7 @@ task start 2
task work 2
task start 3
task work 3
task start 4
task work 2
task work 3
task work 2