cederom commented on PR #22268: URL: https://github.com/apache/nuttx/pull/22268#issuecomment-7206901761
Thanks, the two-step `work_queue_init()` / `work_queue_run()` shape reads well (and matches Zephyr's `k_work_queue_run()`). Comments on 4c73208: **1. `work_queue_free()` vs. a thread that has not reached `work_queue_run()` yet (use-after-free)** `work_queue_free()` waits only for the `nworkers` snapshot, then `kmm_free()`s the queue. A caller thread that was created but has not yet taken the lock in `work_queue_run()` will then call it on freed memory. `-ESHUTDOWN` can only be seen in the short window between `exit = true` and `kmm_free()`, so callers can't rely on it, and the caller has no race-free way to know "my thread is now inside `work_queue_run()`": anything it signals before the call can be preempted before the lock is taken. The doc says the threads "must call work_queue_run() before the queue is freed", but no API lets the caller check that. Options: - Make the contract "exactly `nthreads` threads call `work_queue_run()`": `work_queue_free()` waits for `nthreads` posts, and a late `work_queue_run()` that sees `exit` posts `exsem` and returns `-ESHUTDOWN`. Then `-ESHUTDOWN` is reliable and nothing touches freed memory. The cost is that `free()` blocks until every thread has arrived. - Or keep the current behaviour, but drop `-ESHUTDOWN` from the documented return values and state plainly that calling `work_queue_run()` concurrently with or after `work_queue_free()` is undefined. The same applies to the libc implementation. **2. Workers that die inside the loop** If a `work_queue_run()` thread ends while it is registered (its task exits, or a callback calls `pthread_exit()` / hits a cancellation point), `exsem` is never posted, the slot stays taken, and a later `work_queue_free()` blocks forever. The rst covers the task-exit case. Please also mention it in the header comment of `work_queue_run()`, since kthread workers could not die this way. **3. `work_queue_priority_wq()` before the first worker arrives** Both implementations read `worker[0].pid/tid`. For a `work_queue_init()` queue with no worker yet, that is `INVALID_PROCESS_ID`, so you get `-ESRCH` (kernel) or a `sched_getparam(-1)` error (libc). Please document or handle this. Similarly, `work_queue_setpriority_wq()` now skips empty slots and returns OK, so threads that join later keep their own priority. That's probably fine, but worth a line in the docs. **4. Minor** - Kernel and libc disagree on `work_queue_run()` for a queue made by `work_queue_create()`: the kernel returns `-EBUSY` (all slots taken), libc returns `-EINVAL` (`!run`). The header documents `-EINVAL` for "does not identify a custom queue". Please pick one. - Coverage: nothing in `sim:ostest` exercises the new path. Is apps #4140 meant to become part of ostest? A case where `work_queue_free()` runs while one of the `nthreads` threads has not started would cover (1). - CMake, SMP, and real hardware are untested per the description. SMP matters most here, because of the lock/registration ordering. Apart from that, the refactor (`work_queue_alloc()` / `work_loop()` / `work_queue_release()`) keeps `work_queue_create()` behaviour unchanged as far as I can see: `nworkers = created` on the create path, and libc still joins all `nthreads`. -- 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]
