← index #15982Issue #15192
Related · high · value 3.395
QUERY · ISSUE

Thread on Core 1 on PICO hangs when accessing io

openby kpg141260opened 2024-10-09updated 2025-02-04
bugport-rp2needs-info

Port, board and/or hardware

Raspberry Pi Pico

MicroPython version

MicroPython v1.23.0 on 2024-06-02; Raspberry Pi Pico with RP2040

Reproduction

import os
import time
import _thread


def core1_thread():
    try:
        print("entering core1_thread")
        text = "some text to log"
        # Open log file in append mode
        f = open("test.log", "a")
        f.write(text)
    except OSError as ex:
        print(ex)
    finally:
        f.close()


def create_log():
    """
    Check if the log file exists. If the log file does not exist, create the log file and write a header line.

    Parameters:
        None

    Returns:
        None
    """
    _log_fn = "/test.log"
    try:
        # File exists
        os.stat(_log_fn)
        return
    except OSError:
        idx = _log_fn.rfind("/")
        dir = _log_fn[1:idx]
        if dir and not dir in os.listdir():
            os.mkdir(dir)
        # File does not exist
        try:
            t = f"file {_log_fn} created\n"
            with open(_log_fn, "a") as f:
                f.write(t)
                print(t.rstrip())
        except OSError as ex:
            raise OSError(f"exception {ex}")


def main():
    create_log()
    thread = _thread.start_new_thread(core1_thread, ())

if __name__ == "__main__":
    main()
    time.sleep(1)  # if 'mpremote run', allow thread to run before we soft reset

Expected behaviour

Expected to see 'some text to log' in file test.log on pico.

Observed behaviour

After starting the thread it hangs after printing 'entering core1_thread'.

Additional Information

No, I've provided everything above.

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