Skip to content

Thread does not handle import correct #19682

Description

@tomdean1939

Port, board and/or hardware

Pico

MicroPython version

MicroPython v1.30.0-preview.58.g14e9793c91 on 2026-09-04; Raspberry Pi Pico with RP2040

Reproduction

import time
from machine import mem32
import _thread

def thread_entry(a, b):
    try:
        core_num = mem32[0xd0000000]
    except Exception as e:
        print(f'function thread entry exception {e}')
    print(f'core {core_num} thread {_thread.get_ident()} {a} {b}')

# spawn thread using kw args
id = _thread.start_new_thread(thread_entry, (10, 20))
core_num = mem32[0xd0000000]
print(f'core {core_num} thread {_thread.get_ident()}')

time.sleep(1)

Expected behaviour

Expected no exception. See different behavior based on the order of execution.

Observed behaviour

core 0 thread 1
function thread entry exception name 'mem32' isn't defined
Unhandled exception in thread started by <function thread_entry at 0x20007250>
Traceback (most recent call last):
File "", line 13, in thread_entry
NameError: local variable referenced before assignment

Additional Information

In the code posted above, in the function 'thread_entry' changing mem32 to machine.mem32 resolves the issue.
Alternately, moving 'core_num = mem32[0xd0000000]' to before starting the thread resolves the issue.

Code of Conduct

Yes, I agree

Activity

  1. Josverl commented on Sep 5, 2026

    @Josverl
    SponsorContributor

    Tip: use 'triple backtick'
    ```py
    # code goes here
    ``` around your code snippets to get syntax highlighting and proper indenting.

    You can edit the post above

  2. sosi-deadeye commented on Sep 5, 2026

    @sosi-deadeye

    In some forums you could change the div element to pre to get the whitespace back. But this doesn't work on GitHub. AFIK, whitespace at the beginning are stripped away, if you don't put your code in code tags

  3. tomdean1939 commented on Sep 5, 2026

    @tomdean1939
    Author

    Sorry for the editing.

  4. sosi-deadeye commented on Sep 5, 2026

    @sosi-deadeye

    Sorry for the editing.

    That's what the feature is for :-)

    For example, I've now removed the code so it doesn't appear twice. But you can still see the history.

  5. agatti commented on Sep 6, 2026

    @agatti
    Contributor

    Tip: use 'triple backtick' [...] around your code snippets [...].

    Maybe this reminder could be added to the templates in .github/ISSUE_TEMPLATE?

  6. peterhinch commented on Sep 6, 2026

    @peterhinch
    Contributor

    I pasted this code on an RP2350 and it worked:

    MicroPython v1.28.0 on 2026-04-06; Raspberry Pi Pico2 with RP2350
    Type "help()" for more information.
    >>>
    paste mode; Ctrl-C to cancel, Ctrl-D to finish
    === import time
    === from machine import mem32
    === import _thread
    === 
    === def thread_entry(a, b):
    ===     try:
    ===         core_num = mem32[0xd0000000]
    ===     except Exception as e:
    ===         print(f'function thread entry exception {e}')
    ===     print(f'core {core_num} thread {_thread.get_ident()} {a} {b}')
    === 
    === # spawn thread using kw args
    === id = _thread.start_new_thread(thread_entry, (10, 20))
    === core_num = mem32[0xd0000000]
    === print(f'core {core_num} thread {_thread.get_ident()}')
    === 
    === time.sleep(1)
    === 
    core 0 thread 1
    core 1 thread 2 10 20
    >>> 

    [EDIT]
    I repeated this test on an RP2040 running firmware V1.29 with the following outcome:

    • Pasted, the code ran correctly.
    • Saving the code to the flash as rats.py and issuing import rats at the REPL the error occurred.
    • With an import statement inside the thread_entry function (as below) it ran correctly.
    def thread_entry(a, b):
        from machine import mem32  # Rest of code as above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions