Multithread breaks file operations on pico2w.
Port, board and/or hardware
RPI_PICO2_W
MicroPython version
MicroPython v1.25.0-preview.180.g495ce91ca on 2025-01-06; Raspberry Pi Pico 2 W with RP2350
Reproduction
- copy the below threads using code and run it:
import time
import _thread
global killme
def threadtest():
global killme
print("Other starts")
while True:
if killme:
break
print("másik thread")
time.sleep(3.17)
print("Other ends")
killme = False
try:
new_thread = _thread.start_new_thread(threadtest, ())
print("Main starts")
while True:
print("Main thread")
time.sleep(1)
except KeyboardInterrupt as theInterrupt:
print("Main ends")
killme = True
- run it
- stop it
- try to copy or delete any file with mpremote, it will hang
- powercycle (pull out the usb conenctor and back) and things will work until I run the above code again.
Expected behaviour
Expected to be able to upload code.
Observed behaviour
Was not able to upload code.
Additional Information
When I copied/ran non-threaded code, a simple hello world the problem did not happen.
Code of Conduct
Yes, I agree
RP2 Hanging on file write after soft reboot when a thread was started
Port, board and/or hardware
RP2 Pico
MicroPython version
MicroPython v1.23.0-5.g3613ad962 on 2024-05-31; Raspberry Pi Pico with RP2040
Reproduction
- Copy this code to main.py
- Run the code
- Do soft reboot
If the thread is started before writing to the file, it works. If the thread has not been started, it hangs.
import _thread
import time
thread_running = True
def second_thread():
while thread_running:
print("Second thread running")
time.sleep(1)
#thread = _thread.start_new_thread(second_thread,()) # Works if thread is started here
print("Writing file 1") # Hangs here after soft reboot
with open("test1.bin","w") as fh:
fh.write(b'test')
print("Writing file 2") # Hangs here after soft reboot
with open("test.bin","ab") as fh:
fh.write(b'test')
print("End writing file")
thread = _thread.start_new_thread(second_thread,())
try:
while True:
print("First thread running")
time.sleep(1)
except KeyboardInterrupt:
thread_running = False
time.sleep(2)
Expected behaviour
The file should be written without blocking after a soft reboot whether the thread is started before or after writing the file.
Observed behaviour
After soft reboot, the micro hangs and it needs a hard reset.
Additional Information
I think everything is here. I'm running the code on a custom hardware (compatible with pico), I'll try with a pico whenever I can access one.
Please ask if you want me to try something or you need more info.
Also, I did not experience bugs on 1.21.0 (although I did not try this specific code yet). It seems it happens in 1.22/1.23.
Code of Conduct
Yes, I agree