sched/semaphore: Add debug assert to detect mutex recursion. #18338
+5
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Why change is necessary: Mutex recursion (a task attempting to lock a mutex it already holds) is not allowed in NuttX and indicates a programming error. Currently, such attempts may fail silently or cause unexpected behavior. Adding a debug assertion helps catch these bugs early during development and testing.
What functional part of the code is being changed: The
nxsem_wait_slow()function insched/semaphore/sem_wait.c, which handles the slow path of semaphore/mutex acquisition.How does the change exactly work: A
DEBUGASSERTis added to verify that the current task (obtained vianxsched_gettid()) is not already the holder of the mutex being acquired. The assertion checks the mutex holder bits (excluding the blocking bit) against the current task ID. If a task attempts to recursively lock a mutex it already holds, the assertion will trigger, alerting developers to the bug.Related NuttX Issue reference: None
Related NuttX Apps Issue / Pull Request reference: None
Impact
Is new feature added? Is existing feature changed? YES - Existing feature enhanced. A debug-time safety check is added to the mutex acquisition path to detect illegal recursion attempts.
Impact on user: NO - This is a debug-only assertion (DEBUGASSERT) that only triggers in debug builds. Production builds are unaffected. Users who have mutex recursion bugs will now get clear assertion failures instead of silent failures.
Impact on build: NO - The change is minimal (5 lines added) and does not affect build process or configuration.
Impact on hardware: NO - This is a pure software change in the scheduler/semaphore subsystem with no hardware-specific implications.
Impact on documentation: NO - No documentation updates required. This is an internal debug enhancement.
Impact on security: NO - This change improves robustness by catching programming errors earlier, which is a positive security practice.
Impact on compatibility: NO - Backward compatible. Debug assertions are non-breaking; they only help catch bugs.
Anything else to consider: This change follows NuttX best practices for defensive programming. The assertion is placed at the critical point where mutex recursion would be detected, making it effective for catching bugs during development and testing.
Testing
I confirm that changes are verified on local setup and works as intended:
qemu-system-arm -M mps3-an547 -m 2G -nographic -kernel nuttx.binTesting logs before change:
Testing logs after change:
Test Result: PASS - All ostest tests completed successfully, including mutex tests. The debug assertion does not interfere with normal mutex operation. No assertion failures detected, indicating no mutex recursion attempts in the test suite.
PR verification Self-Check