← index #12651Issue #9929
Related · high · value 3.396
QUERY · ISSUE

ESP32 asyncio run in _thread

openby stragaopened 2023-10-10updated 2023-10-24
bugport-esp32

I'm using MicroPython v1.22.0-preview.4.g9f835df35.dirty on an ESP32 device without PSRAM, with ESP-IDF v5.0.2.

The problem arises when running asyncio in a thread. Here are the steps that lead to the issue:

  • Run:

    loop = asyncio.get_event_loop()
    _ = _thread.stack_size(12 * 1024)
    _thread.start_new_thread(loop.run_forever, ())

  • Connect to WIFI successfully.

Inside the loop, connect to MQTT using asyncio:
reader, writer = await asyncio.wait_for(asyncio.open_connection(self.addr, self.port), 5)

  • Run the WebREPL service.

  • Try connecting to WebREPL and typing commands or a lot time some "sdfsdfdsfds".

However, during this process, the following issues occur:

Loss of ping and disconnection from Wi-Fi:
Disconnected, received disassociation: no activity (4), signal strength -35
WebREPL raises an exception in the read() method and deactivates with the error:
OSError: [Errno 113] ECONNABORTED

<img width="922" alt="image" src="https://github.com/micropython/micropython/assets/34532/905fce67-49eb-4dd7-866e-6242e69bd590">

  • Interestingly, when running the same code without a _thread, it works normally. Additionally, when using an ESP32 with PSRAM and thread, the code also works normally too.
CANDIDATE · ISSUE

ESP32 - Bluetooth - BLE scan and WIFI not works same time.

openby stragaopened 2022-11-12updated 2023-10-17
bug

ESP32 rev1 and rev3. Board without psram.
Try with ESP-IDF, 4.4.1 and 4.4.3, micropython from master

Suppose running asyncio in the thread. Task call ble.scan. Wifi works not good. At first look ok, But if you try from FTP download/upload some or other. Wifi stops working normally.

<img width="646" alt="image" src="https://user-images.githubusercontent.com/34532/201466036-86465512-b6c1-4729-b9ea-4212930859c6.png">

If run asyncio without thread. All works normally.
<img width="754" alt="image" src="https://user-images.githubusercontent.com/34532/201466074-b0f34aa4-281b-44d9-a441-ceceec32f8cc.png">

Firmware:
4m_straga_core.zip

main.py and ftp.py for test.

test_ble.zip

I test with asyncio with out thread, wifi stop work too. But only if load from FTP big file.

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