aviralgarg05 opened a new pull request, #18288:
URL: https://github.com/apache/nuttx/pull/18288

   Move the assertion check in `nxsem_wait` to after the fast path logic. This 
prevents `Idle_Task` from asserting when acquiring an uncontended 
semaphore/mutex (e.g. for logging), while still preventing it from waiting on a 
contended one.
   
   Fixes: #17865
   
   ## Summary
   
   The `nxsem_wait` function previously contained a `DEBUGASSERT` at the very 
beginning that prohibited the `Idle_Task` or an interrupt handler from calling 
the function entirely. However, with the introduction of atomic "fast-path" 
acquisition, it is safe for these contexts to acquire a semaphore or mutex if 
it is currently available (uncontended), as this does not result in a context 
switch or blocking.
   
   This change moves the assertion to the point just before calling 
`nxsem_wait_slow`. This ensures that:
   
   1. `Idle_Task` and interrupt handlers can successfully acquire available 
semaphores/mutexes via the fast path.
   2. If the semaphore is not available, the assertion still triggers to 
prevent these invalid contexts from entering the blocking slow-path.
   
   This specifically resolves a crash observed in the LVGL simulator when the 
mouse enters the page and the `Idle_Task` or related kernel threads trigger an 
assertion while performing operations (like logging) that involve semaphores.
   
   ## Impact
   
   - **Users:** Fixes a recurring crash in the `sim:lvgl_lcd` configuration and 
potentially other simulator/kernel scenarios.
   - **Stability:** Improves robustness of logging and other core services when 
called from low-level contexts, provided the resources are not contended.
   - **Compatibility:** No impact on existing build process or hardware 
compatibility.
   
   ## Testing
   
   - **Coding Style:** Verified compliance using `./tools/checkpatch.sh -f 
libs/libc/semaphore/sem_wait.c`.
   - **Static Analysis:** Thoroughly reviewed the `nxsem_wait` implementation 
in `libs/libc/semaphore/sem_wait.c` to ensure the logic flow correctly bypasses 
the assertion only upon successful atomic acquisition.
   - **Verification:** Confirmed that the fix addresses the root cause 
identified in the issue description (Assertion failed at file: 
semaphore/sem_wait.c:146 task: Idle_Task).
   
   Host: macOS (Apple Silicon)
   Target: Simulator (sim:lvgl_lcd)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to