cederom commented on PR #4127:
URL: https://github.com/apache/nuttx-apps/pull/4127#issuecomment-7136466019

   Thanks for adding a regression test for this. I traced the scenario through 
the current code and the test does catch the bug:
   
   - `flockfile()` → `nxrmutex_lock()`. The holder thread exits with 
`fs_lock.count == 1` and the mutex holder set to its TID.
   - `fflush()` in the main thread → `flockfile()` → `nxmutex_lock()` → 
`nxsem_wait()`, which sees that `nxsched_get_tcb(holder)` is NULL and takes the 
mutex over. But `nxrmutex_lock()` then does `++count` on the stale count (1 → 
2), so `funlockfile()` only brings it back to 1 and never releases the mutex.
   - `ftrylockfile()` in the second thread then gets `-EAGAIN`, so the test 
prints an error and asserts (it doesn't hang, and the final `fclose()` doesn't 
deadlock either, because the main thread still holds the lock).
   
   Comments:
   
   1. **Dependency on the nuttx fix.** The commit message says "This needs the 
recursive mutex fix in nuttx" but doesn't say which one. Please link the 
apache/nuttx PR (or commit) in the commit message and the PR description. This 
should only be merged after that fix lands, otherwise ostest hits 
`ASSERT(false)` in every CI config that runs ostest with `CONFIG_FILE_STREAM=y`.
   
   2. **The test depends on `fflush()` internals.** The takeover only happens 
on the *blocking* path. `nxsem_trywait_slow()` returns `-EAGAIN` for a mutex 
held by a dead holder, so if `fflush()` ever stops locking (or someone replaces 
it with `ftrylockfile()`), the test stops testing what it means to test. I'd 
make the intent explicit:
   
      ```c
      /* Take the lock over from the exited thread (blocking lock path)
       * and release it again.
       */
   
      flockfile(stream);
      funlockfile(stream);
      ```
   
   3. **Unchecked `pthread_create()`.** If it fails, `pthread_join()` gets an 
uninitialized `pthread_t`. Other ostest cases (e.g. `robust.c`) check the 
status, print an error and `ASSERT(false)`. Please do the same for both calls.
   
   4. Nit: in the `fdopen()` failure path, `ASSERT(false)` doesn't return, so 
`close(fd); return;` is unreachable. (If `dup()` failed, `fd` is -1 anyway.) 
Either drop the cleanup or move it before the assert. Checking `dup()` 
separately would also give a clearer message.
   
   5. Minor: dead-holder recovery works by looking up the TID, so the takeover 
relies on the holder's TID not being reused before the main thread locks the 
stream. That's true here because no thread is created between the 
`pthread_join()` and the takeover, but a one-line comment would help whoever 
reorders this test later.
   
   6. NuttX-specific behaviour: POSIX doesn't define what happens to a stream 
lock held by a thread that exits (on glibc the lock stays held forever). It's 
fine that ostest checks NuttX's recovery behaviour, but please state that in 
the file's comment, so nobody "fixes" the test toward POSIX.
   
   The build integration looks right: `CONFIG_FILE_STREAM` is nested inside the 
`!CONFIG_DISABLE_PTHREAD` block in the Makefile, CMakeLists.txt, `ostest.h` and 
`ostest_main.c`.


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