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

   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## Summary
   
   A thread that exits while it holds a recursive mutex leaves its thread id
   and its lock count in the mutex. This happens, for example, when a thread
   is cancelled or calls `pthread_exit()` with a stream locked by
   `flockfile()` or inside `printf()`. The next `nxrmutex_lock()` takes the
   mutex over in `nxsem_wait_slow()`, which treats a holder that no longer
   exists as no holder, but adds its own lock to the old count. Its last
   unlock then leaves the count above zero and never releases the mutex, so
   every other thread blocks on it, or fails to try-lock it, forever.
   
   Start the count again when the mutex itself is acquired, in
   `nxrmutex_lock()`, `nxrmutex_ticklock()`, `nxrmutex_clocklock()` and
   `nxrmutex_timedlock()`. `nxrmutex_trylock()` is not changed: it fails
   while the id of the old holder is set, so it cannot take the mutex over.
   
   ## Impact
   
   Bug fix only. No change for a recursive mutex whose holders unlock it
   before they exit. No impact on the build system, documentation or
   hardware.
   
   ## Testing
   
   Build host: Linux x86_64.
   
   Regression test: apache/nuttx-apps#APPS_PR (`testing/ostest`: a thread
   exits with a stream locked by `flockfile()`, the main thread flushes the
   stream, then another thread calls `ftrylockfile()`).
   
   `sim:nsh` + `CONFIG_TESTING_OSTEST`, ostest, before this PR:
   
   ```
   user_main: flockfile test
   flockfile_test: Exiting a thread that holds the stream lock
   flockfile_test: Locking the stream from another thread
   flockfile_test: ERROR: the stream stayed locked
   dump_assert_info: Current Version: NuttX  13.1.1-RC0 b5db91fe0da Nov 24 2026 
18:01:53 sim
   ```
   
   With this PR, `sim:nsh` and `qemu-armv8a:nsh` (QEMU), full ostest:
   
   ```
   user_main: flockfile test
   flockfile_test: Exiting a thread that holds the stream lock
   flockfile_test: Locking the stream from another thread
   ...
   ostest_main: Exiting with status 0
   ```
   
   `sim:nsh` builds with Make and CMake, and `qemu-armv8a:nsh` with Make,
   with no warning. Not tested on real hardware.


-- 
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