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

   ### What's good
   - **It targets the real bug.** Before the fix, `local_release()` dropped the 
lock around `local_subref(accept)`. A higher-priority client woken by 
`local_free()` could then `connect()` while the listener was still 
`LOCAL_STATE_LISTENING`. Giving the client thread a higher priority makes this 
happen reliably on a single CPU.
   - **With the fix, the result is fixed, not timing-dependent.** The client's 
`connect()` blocks in `local_lock()` until `close()` finishes. Then no listener 
matches, and `local_connect()` returns `-ECONNREFUSED`. The `ECONNREFUSED || 
ENOENT` check covers this.
   - **It fails safe.** If the client isn't in `recv()` yet when `close()` runs 
(for example on SMP), `recv()` returns 0 at once and the test still passes on a 
fixed kernel. It just doesn't hit the race that time. If the client never 
wakes, the existing watchdog catches it. `pthread_join` runs before any of the 
result checks, so a failed assert can't leave the thread running.
   - `errno` is saved across `close(fd)` in `local_dgram_do_reconnect()`. Good.
   
   ### Changes I'd ask for
   1. **Check the `recv()` result in `local_dgram_do_reconnect()`.** Right now 
it's ignored. If `recv()` returns early for any other reason (EINTR, a 
regression that makes it fail), the thread connects to a listener that's still 
open. That succeeds (backlog 4), and the test fails as "connect() returned 0", 
which wrongly points at the lock bug. The sibling 
`test_local_dgram_stream_listenclose` already checks `ret == 0 || errno == 
ECONNRESET`. Doing the same here, with a separate return code for that case, 
would keep failures clear.
   2. **Don't copy `local_dgram_job_main()`.** `local_dgram_reconnect_main()` 
is the same function minus the `usleep()`, and the test fills in `job` by hand. 
That skips `job->target` and `job->msg`, which `local_dgram_job_arg()` sets. A 
cleaner option is to add an optional `pthread_attr_t *` (or a priority offset) 
to `local_dgram_job_arg()` in `test_local_dgram_common.c`, then call it with 
`ms = 0`. Then there's only one way to start a job thread.
   3. **Check the return values of the `pthread_attr_*` calls.** 
`param.sched_priority++` gives an invalid priority if the test already runs at 
`SCHED_PRIORITY_MAX`. In that case `pthread_attr_setschedparam()` fails 
silently, and the failure only shows up later at `pthread_create()` with a 
vague message. Checking the return values, or limiting the priority to 
`sched_get_priority_max(policy)`, makes this clear.
   
   ### Nits
   - `return -2` when `socket()` fails gets reported as "-2 != -1". A comment, 
or a separate check, would make that easier to read.
   - The new test uses `SOCK_CLOEXEC` but `listenclose` next to it doesn't. 
It's harmless, but pick one style.
   - `#include <sched.h>` isn't strictly needed because `pthread.h` already 
brings in `struct sched_param`. Keeping it is fine.
   - The "Let the client reach its recv() also on another CPU" comment could 
say that on SMP the test may not hit the race at all, so it won't catch the bug 
there every time. That's acceptable, but worth stating.


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