You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Threads were hanging when executing Python functions due to two critical bugs
in the thread initialization path when MICROPY_ZEPHYR_THREADING is enabled:
Bug #1 - C Stack Initialization Corruption (runtime.h):
zephyr_entry() correctly sets C stack bounds using actual Zephyr thread
stack info, but mp_thread_init_state() was then overwriting those bounds
by calling mp_cstack_init_with_top(ts + 1, stack_size). The ts + 1 address
points to memory after a local variable, causing mp_cstack_check() to
calculate garbage values and potentially raise recursion depth errors.
Bug #2 - Thread State Pointer Corruption (modthread.c):
zephyr_entry() correctly sets TLS to heap-allocated self->thread_state,
but thread_entry() creates a local variable mp_state_thread_t ts and calls
mp_thread_init_state(&ts, ...) which overwrites the TLS pointer to point
to this local stack variable. When the stack frame changes, all
MP_STATE_THREAD() accesses point to invalid memory.
Root cause: For Zephyr threading there are two entry points:
1. zephyr_entry() - Zephyr thread entry that initializes MicroPython
2. thread_entry() - Generic MicroPython thread entry
The bugs occurred because thread_entry() was reinitializing everything
that zephyr_entry() had already correctly initialized, causing double
initialization and memory corruption.
Solution: Modified thread_entry() to skip thread state allocation,
C stack initialization, and mp_thread_start() call when Zephyr threading
is enabled, as these are already handled by zephyr_entry(). Thread_entry()
now only handles GIL, NLR, and function execution for Zephyr threading.
Changes:
- py/runtime.h: Skip mp_cstack_init_with_top() for Zephyr threading
- py/modthread.c: Skip thread state init and mp_thread_start() for Zephyr
Tested on STM32 NUCLEO_F429ZI (STM32F429ZITx).
Threads now successfully execute Python functions.
Signed-off-by: Andrew Leech <[email protected]>
0 commit comments