RP2 asyncio stream IO hangs: regression
Port, board and/or hardware
RP2040 or RP2350
MicroPython version
Fails on release builds >= 22.0
Bug seems RP2-specific: the script runs on STM32 (Pyboard 1.1) and ESP32-S3 with current firmware (1.27).
Reproduction
Either paste the script at the REPL or run from a file foo.py (mpremote mount . "exec import foo")
import asyncio # as asyncio
import time
import io
MP_STREAM_POLL_RD = const(1)
MP_STREAM_POLL = const(3)
MP_STREAM_ERROR = const(-1)
class MillisecTimer(io.IOBase):
def __init__(self):
self.end = 0
self.sreader = asyncio.StreamReader(self)
def __iter__(self):
await self.sreader.read(1)
def __call__(self, ms):
self.end = time.ticks_add(time.ticks_ms(), ms)
return self
def read(self, _):
return b"a"
def ioctl(self, req, arg):
ret = MP_STREAM_ERROR
#time.sleep_us(2_000)
if req == MP_STREAM_POLL: # hangs HERE
ret = 0
if arg & MP_STREAM_POLL_RD:
if time.ticks_diff(time.ticks_ms(), self.end) >= 0:
ret |= MP_STREAM_POLL_RD
return ret
async def timer_test(n):
timer = MillisecTimer()
for x in range(n):
await timer(100) # Pause 100ms
print(x)
asyncio.run(timer_test(20))
Expected behaviour
Expected to print integers from 0 to 19.
Observed behaviour
Hangs on the line commented with "hangs HERE".
Behaviour can be "fixed" by uncommenting the 2ms delay.
Additional Information
No, I've provided everything above.
Code of Conduct
Yes, I agree
asyncio terminates unexpectedly
Port, board and/or hardware
Unix build (also verified on RP2 2)
MicroPython version
MicroPython v1.24.1 on 2024-11-29; Raspberry Pi Pico2 with RP2350
Reproduction
Paste the following at the REPL
import asyncio
evt = asyncio.Event()
async def foo():
data = [1,2,3]
for d in data:
print(d)
await asyncio.sleep(1)
await evt.wait() # Should hang
async def main():
await foo()
asyncio.run(main())
Expected behaviour
Should print 1, 2, 3 then hang. This happens under CPython 3.10.12
1
2
3
Observed behaviour
Under MP, after expected output, it returns to the REPL.
1
2
3
>>>
Additional Information
This may be related to https://github.com/micropython/micropython/issues/16318
I am encountering the phenomenon of premature return in a number of situations, notably in test scripts where there are no pending tasks. I have never observed it in applications where there are multiple concurrent tasks.
Code of Conduct
Yes, I agree