summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2020-08-20 23:13:25 +1000
committerDamien George <damien@micropython.org>2020-08-22 12:17:06 +1000
commit55c76eaac12af4e93f8de11bb25c835e8b65c623 (patch)
treecf3e4d1ccad90737a076548dddbc84acb8c5dbc6
parent20948a3d54ff560a3d029b8bfc9761dcbbad9312 (diff)
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>
-rw-r--r--extmod/uasyncio/core.py2
-rw-r--r--tests/extmod/uasyncio_fair.py1
-rw-r--r--tests/extmod/uasyncio_fair.py.exp1
3 files changed, 3 insertions, 1 deletions
diff --git a/extmod/uasyncio/core.py b/extmod/uasyncio/core.py
index 689487d36..045b4cd13 100644
--- a/extmod/uasyncio/core.py
+++ b/extmod/uasyncio/core.py
@@ -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
diff --git a/tests/extmod/uasyncio_fair.py b/tests/extmod/uasyncio_fair.py
index 9b04454bc..e0ee811a9 100644
--- a/tests/extmod/uasyncio_fair.py
+++ b/tests/extmod/uasyncio_fair.py
@@ -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()
diff --git a/tests/extmod/uasyncio_fair.py.exp b/tests/extmod/uasyncio_fair.py.exp
index 4428943f4..b4b6481db 100644
--- a/tests/extmod/uasyncio_fair.py.exp
+++ b/tests/extmod/uasyncio_fair.py.exp
@@ -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