Skip to content

asyncio terminates unexpectedly #16569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
peterhinch opened this issue Jan 11, 2025 · 3 comments
Closed

asyncio terminates unexpectedly #16569

peterhinch opened this issue Jan 11, 2025 · 3 comments
Labels

Comments

@peterhinch
Copy link
Contributor

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 #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

@peterhinch peterhinch added the bug label Jan 11, 2025
@peterhinch
Copy link
Contributor Author

peterhinch commented Feb 1, 2025

To add a few observations.

The problem goes away if a second task is launched that simply does a repeated async sleep. This explains why the fault only occurs in simple scripts [EDIT] see below.

When the fault occurs the run_until_complete loop quits here. It seems that while paused on

    await evt.wait()  # Should hang

there is no cur_task, causing run_until_complete to quit prematurely.

While the example script is artificial I've encountered this bug a few times in simple scripts intended to test applications. The work-round is trivial, but the tendency for premature exit may confuse beginners.
[EDIT}
I have now encountered the bug in a fairly large application. The circumstances were involved but the behaviour was deterministic. In response to a particular condition while one task was waiting on an Event the application terminated unexpectedly.

The workround was to replace the Event with a ThreadSafeFlag. The application is pure asyncio with no threads or ISR's.

@Baker68
Copy link

Baker68 commented Feb 10, 2025

Also present on MicroPython v1.24.0 on ESP32

yoctopuce added a commit to yoctopuce/micropython that referenced this issue Feb 17, 2025
…lete.

The issue is described in details in
micropython#16759

This amended commit also fixes these issues:
micropython#16569
micropython#16318

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
yoctopuce added a commit to yoctopuce/micropython that referenced this issue Feb 18, 2025
The issue is described in details in
micropython#16759

This amended commit also fixes these issues:
micropython#16569
micropython#16318

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
yoctopuce added a commit to yoctopuce/micropython that referenced this issue Feb 18, 2025
The issue is described in details in
micropython#16759

This amended commit also fixes these issues:
micropython#16569
micropython#16318

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
yoctopuce added a commit to yoctopuce/micropython that referenced this issue Mar 3, 2025
The issue is described in details in
micropython#16759

This amended commit also fixes these issues:
micropython#16569
micropython#16318

Test cases have been updated to document the unauthorized
use of Event.set() from soft IRQ, and to be skipped in
unsupported environments (webassembly and emit native).

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
yoctopuce added a commit to yoctopuce/micropython that referenced this issue Mar 5, 2025
The issue is described in details in
micropython#16759

This amended commit also fixes these issues:
micropython#16569
micropython#16318

Test cases have been updated to document the unauthorized
use of Event.set() from soft IRQ, and to be skipped in
unsupported environments (webassembly and emit native).

Code has been improved according to review from dpgeorge.

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
yoctopuce added a commit to yoctopuce/micropython that referenced this issue Mar 5, 2025
The issue is described in details in
micropython#16759

This amended commit also fixes these issues:
micropython#16569
micropython#16318

Test cases have been updated to document the unauthorized
use of Event.set() from soft IRQ, and to be skipped in
unsupported environments (webassembly and emit native).

Code has been improved according to review from dpgeorge.

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
dpgeorge pushed a commit to yoctopuce/micropython that referenced this issue May 7, 2025
This commit fixes three open issues related to the asyncio scheduler
exiting prematurely when the main task queue is empty, in cases where
CPython would not exit (for example, because the main task is not done
because it's on a different queue).

In the first case, the scheduler exits because running a task via
`run_until_complete` did not schedule any dependent tasks.

In the other two cases, the scheduler exits because the tasks are queued in
an event queue.

Tests have been added which reproduce the original issues.  These test
cases document the unauthorized use of `Event.set()` from a soft IRQ, and
are skipped in unsupported environments (webassembly and native emitter).

Fixes issues micropython#16759, micropython#16569 and micropython#16318.

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
@dpgeorge
Copy link
Member

dpgeorge commented May 7, 2025

Fixed by bdb7e03

@dpgeorge dpgeorge closed this as completed May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants