Incorrect assertion on mutex enter-count in async_context_execute_sync() cross-core path #2527
Labels
No labels
bazel
bluetooth
board settings
boot_stage2
bug
build
c++
can't reproduce
clang
clib-interface
cmsis
code cleanliness
code size
compiler version issues
cyw43_driver
debug
discussion
documentation
duplicate
dynamic_clock
elf2uf2
enhancement
errata
filesystem
flash layout
floating point
FreeRTOS
hardware_adc
hardware_claim
hardware_clocks
hardware_dma
hardware_exception
hardware_flash
hardware_gpio
hardware_i2c
hardware_irq
hardware_pio
hardware_pwm
hardware_rcp
hardware_rtc
hardware_spi
hardware_sync
hardware_uart
hardware_vreg
hardware_watchdog
hardware_xosc
help wanted
host
IAR
installation
invalid
linker scripts
lto
lwip
mbedtls
missing API
needs reporter detail
networking
newlib
newlib-multicore
non-c
non-cmake
non-gcc
openocd
orphan PR
OS integration
pico_async_context
pico_base
pico_binary_info
pico_flash
picolibc
pico_lwip
pico_malloc
pico_multicore
pico_printf
pico_runtime
pico_stdio
pico_stdio_usb
pico_sync
pico_time
pico_util
pioasm
port
question
review
review2
rtos interop
runtime init
static library
svd
test
tinyusb
vscode
wifi integration
wontfix
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-raspberrypi/pico-sdk#2527
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
TL;DR
The function async_context_threadsafe_background_execute_sync() incorrectly asserts that the recursive mutex is completely unentered (i.e.,
enter_count == 0) during cross-core invocations. However, it fails to consider who owns the mutex. As a result, it prohibits valid scenarios where the target core (e.g., the async core) legitimately holds the lock while doing its work.This leads to hard assertion failures and makes the function unusable under typical dual-core setups involving background task scheduling.
Description
In multicore builds, async_context_threadsafe_background_execute_sync() uses this assertion in the cross-core execution path:
This assertion only checks whether the recursive mutex's
enter_countis zero. It does not verify which core, if any, owns the mutex. This check is too strict for valid cross-core calls.For example, the async core (e.g., Core 1) may legally be processing background work while holding the lock. In this situation, a cross-core call from Core 0 should be allowed to run execute_sync(). However, the assertion fails because
enter_countis non-zero, even though the usage is valid and deadlock-free.This behaviour contradicts the SDK's documentation:
The documentation only states that the method should not be called while holding the lock, not that the other core must not acquire the lock.
Instead, the check should ensure that the calling core does not already own the lock.
Steps to Reproduce
async_context_threadsafe_background_tto run on Core 1.async_context_execute_sync()while Core 1 is processing scheduled work viaasync_context_add_at_time_worker_in_ms().mutex_state.Expected Behaviour
Actual Behaviour
The assertion fails even when the calling core does not hold the mutex.
GDB inspection reveals that:
owneris1(i.e., Core 1),enter_countis1,This confirms that the assertion triggers due to activity on the other core, not the one making the call.
async_context_add_at_time_worker_in_ms()which holds the lock on the async core.For example:
This indicates that the assertion is triggered during normal background operation, not a logic error by the user.
Environment
Additional Information
recursive_mutex_tto prevent compiler optimisations from eliding reads.self->core_num), not by the calling core.Comparison with RTOS implementation
The FreeRTOS variant of
async_context_execute_sync()performs an ownership-aware check:fix merged into develop