Skip to content

Commit 55c76ea

Browse files
committed
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>
1 parent 20948a3 commit 55c76ea

File tree

3 files changed

+3
-1
lines changed

3 files changed

+3
-1
lines changed

extmod/uasyncio/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __next__(self):
5353
# Use a SingletonGenerator to do it without allocating on the heap
5454
def sleep_ms(t, sgen=SingletonGenerator()):
5555
assert sgen.state is None
56-
sgen.state = ticks_add(ticks(), t)
56+
sgen.state = ticks_add(ticks(), max(0, t))
5757
return sgen
5858

5959

tests/extmod/uasyncio_fair.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ async def main():
2222
t1 = asyncio.create_task(task(1, -0.01))
2323
t2 = asyncio.create_task(task(2, 0.1))
2424
t3 = asyncio.create_task(task(3, 0.2))
25+
t3 = asyncio.create_task(task(4, -100))
2526
await asyncio.sleep(0.5)
2627
t1.cancel()
2728
t2.cancel()

tests/extmod/uasyncio_fair.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ task start 2
33
task work 2
44
task start 3
55
task work 3
6+
task start 4
67
task work 2
78
task work 3
89
task work 2

0 commit comments

Comments
 (0)