Thread does not handle import correct · Issue #19682 · micropython/micropython · GitHub
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

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