← index #10867Issue #3242
Related · high · value 2.043
QUERY · ISSUE

uasyncio StreamReader fails if UART times out.

openby peterhinchopened 2023-02-26updated 2023-06-07
bug

I'm unsure if this qualifies as a bug as there is an obvious workround.

The issue arose in this report. If the UART receiver times out the device returns None which causes uasyncio to fail here.

CANDIDATE · ISSUE

uasyncio StreamReader/StreamWriter can fail if a coro blocks on an awaitable

closedby peterhinchopened 2017-07-25updated 2017-08-20

The following runs on a Pyboard with a link between X1 and X2. If the Bar class is changed such that the sleep time is zero, the UART I/O does not take place.

# Link X1 and X2 to test.

import uasyncio as asyncio
from pyb import UART
uart = UART(4, 9600)

async def sender():
    swriter = asyncio.StreamWriter(uart, {})
    while True:
        await swriter.awrite('Hello uart\n')
        await asyncio.sleep(2)

async def receiver():
    sreader = asyncio.StreamReader(uart)
    while True:
        res = await sreader.readline()
        print('Recieved', res)

class Bar():
    def __await__(self):
        while True:
            yield from asyncio.sleep_ms(5)  # Change this to 0 and the app fails

    __iter__ = __await__

bar = Bar()

async def foo():
    print('Starting foo. Should never finish.')
    await bar
    print('foo finished.')

loop = asyncio.get_event_loop()
loop.create_task(foo())
loop.create_task(sender())
loop.create_task(receiver())
loop.run_forever()

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied