← index #16554Issue #15192
Likely Duplicate · medium · value 0.546
QUERY · ISSUE

Multithread breaks file operations on pico2w.

openby tudorapoopened 2025-01-08updated 2025-03-03
bugport-rp2

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

  1. 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
  1. run it
  2. stop it
  3. try to copy or delete any file with mpremote, it will hang
  4. 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

CANDIDATE · ISSUE

RP2 Hanging on file write after soft reboot when a thread was started

closedby eleroyopened 2024-06-02updated 2025-02-07
bug

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

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