Skip to content

testing/ostest: Test work_queue_init() and work_queue_run() - #4140

Open
chengkai15 wants to merge 1 commit into
apache:masterfrom
chengkai15:ostest-wqueue-run
Open

chengkai15 wants to merge 1 commit into
apache:masterfrom
chengkai15:ostest-wqueue-run

Conversation

@chengkai15

@chengkai15 chengkai15 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Note: Please adhere to Contributing Guidelines.

Depends-On: apache/nuttx/pull/22268

Summary

Regression test for work_queue_init() and work_queue_run() (apache/nuttx#22268).

A queue of work_queue_init(2) is served by two pthreads of the test task that call work_queue_run(). The test checks that:

  • work queued before the threads start runs;
  • it runs in the task: getpid() is the pid of the task, and a file descriptor that the task opened can be written;
  • both threads serve the queue at the same time (sync and parallel cancel tests), and a third work_queue_run() returns -EBUSY;
  • the priority tests pass on the queue;
  • work_queue_run() returns 0 in both threads after work_queue_free();
  • work_queue_free() does not wait for a slot that no thread took.

Impact

Testing

sim:ostest (flat), full ostest (4 loops, no errors):

wqueue_test: work_queue_run
wqueue_test: worker pid = 5, expect = 5, write = 1
wqueue_test: sync cancel finished = 1, expect = 1
wqueue_test: parallel callbacks = 2, expect = 2
wqueue_test: work_queue_run() = 0, expect = 0
wqueue_test: work_queue_run() = 0, expect = 0
wqueue_test: work_queue_run done

mps2-an500:citest_protected (protected) + CONFIG_LIBC_USRWORK, qemu-system-arm, ostest wqueue:

wqueue_test: work_queue_run
wqueue_test: worker pid = 5, expect = 5, write = 1
wqueue_test: work_queue_run() = 0, expect = 0
wqueue_test: work_queue_run() = 0, expect = 0
wqueue_test: work_queue_run done

qemu-armv7a:citest_knsh (kernel) + CONFIG_LIBC_USRWORK, qemu-system-arm, ostest wqueue:

wqueue_test: work_queue_run
wqueue_test: worker pid = 4, expect = 4, write = 1
wqueue_test: work_queue_run() = 0, expect = 0
wqueue_test: work_queue_run() = 0, expect = 0
wqueue_test: work_queue_run done

On protected and kernel, ostest wqueue fails on master before it reaches this case, in existing checks: cancelling idle work on a libc custom queue does not return -ENOENT (both builds), and the callbacks of the predefined USRWORK queue do not run (kernel). For these two runs those checks were disabled locally.

Not tested: CMake, SMP, real hardware.

@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx-apps/actions/runs/45625625849

@cederom

cederom commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Thanks, the test reuses the existing helpers well, and parallel_cancel_test() really does prove that both work_queue_run() threads serve the queue at once. Comments, most important first:

1. work_queue_run() on a work_queue_create() queue: flat and user mode disagree, and the test doesn't cover it

With #22268 at 4c73208:

  • kernel / flat (sched/wqueue/kwork_thread.c): the guard is wqueue == NULL || !wqueue->dynamic. A work_queue_create() queue is dynamic, so the call goes on to the slot scan. All slots have a pid, so it returns -EBUSY. (If a slot was ever left at INVALID_PROCESS_ID, the caller would quietly become an extra worker.)
  • libc (work_usrthread.c): the guard is !wqueue->run, so the same call returns -EINVAL.

The header says work_queue_run() is for "a queue created by work_queue_init()" and lists -EINVAL for a handle that "does not identify a custom queue". I'd add a check on one of the work_queue_create() queues that already exist in wqueue_test_entry():

ASSERT(work_queue_run(wq) == -EINVAL);

and fix the kernel side in #22268 to match, e.g. with a run flag like the libc one. As things stand this check would fail on sim:ostest and pass on the protected/kernel configs.

2. A regression in the -EBUSY check hangs ostest instead of failing

ASSERT(work_queue_run(wqueue) == -EBUSY) runs on the test thread. If a regression lets it take a slot, the test thread runs the work loop forever, and ostest stalls with no ASSERT message. Making this third call from a run_thread_start() thread that posts a semaphore when it returns, then waiting with wait_sem() (2 s timeout), turns the hang into a clean failure.

3. The new skip in work_queue_setpriority_wq() is untested

#22268 adds the INVALID_PROCESS_ID skip, and the second queue here (one slot never taken) is exactly the case it handles. The API is flat/kernel only, so something like this would cover it:

#ifdef CONFIG_BUILD_FLAT
  ASSERT(work_queue_setpriority_wq(wqueue, CUSTOM_PRIORITY + 1) == OK);
  ASSERT(work_queue_priority_wq(wqueue) == CUSTOM_PRIORITY + 1);
#endif

Without the skip, nxsched_set_param(INVALID_PROCESS_ID, ...) fails, so this check would catch a regression.

4. Second sub-test: rwork is reused but not checked again

rwork.pid and rwork.nwritten still hold the values from the first sub-test, and nothing asserts them after the second wait_sem(). Either reset them to -1 and repeat the getpid()/write() asserts, or drop the expectation and say so. An ASSERT(work_available(&work)) before reusing work would also make the reuse explicit.

5. Smaller, optional

  • -EDEADLK from a work_queue_run() worker isn't covered: calling work_queue_free() from a callback on this queue, as self_free_test() already does for a created queue. It's cheap to add, and it checks that the pid/tid stored by work_queue_run() is the one work_queue_free() compares against.
  • -ESHUTDOWN can only be reached through a race, so leaving it untested is fine. It would help to say so in the commit message, since the message lists the other return codes.
  • O_CLOEXEC isn't needed for /dev/null here, though it does no harm.

The -EBUSY / -EINVAL divergence in (1) is the one I'd like resolved, together with #22268, before merge. The rest are suggestions.

Serve a queue of work_queue_init() with two pthreads of the test task
that call work_queue_run().  Check that work queued before the threads
start runs, that it runs in the task (getpid() is the pid of the task
and a file descriptor that the task opened can be written), that both
threads serve the queue at the same time, that a third
work_queue_run() returns -EBUSY, that a worker cannot free its own
queue (-EDEADLK), and that work_queue_run() returns 0 after
work_queue_free().  The -EBUSY call runs in its own thread, so a
regression fails instead of hanging ostest.

Then check that work is served while a slot is still empty, that the
priority of such a queue can be changed in the flat build, and that a
thread that calls work_queue_run() after work_queue_free() gets
-ESHUTDOWN while work_queue_free() waits for it.  work_queue_run() on a
queue of work_queue_create() returns -EINVAL.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
@chengkai15

Copy link
Copy Markdown
Contributor Author

@cederom thanks, all points are addressed in the new push:

  1. work_queue_run() on a work_queue_create() queue is checked with ASSERT(work_queue_run(wq) == -EINVAL). sched/wqueue: Add work_queue_init() and work_queue_run() nuttx#22268 now returns -EINVAL from the kernel too, with a run flag.
  2. The -EBUSY call runs in a run_thread_start() thread and is joined with a wait_sem() timeout, so a regression fails instead of hanging ostest.
  3. The work_queue_setpriority_wq() skip of an empty slot is covered under CONFIG_BUILD_FLAT, as you suggested.
  4. The second sub-test resets rwork.pid / rwork.nwritten to -1 and repeats the getpid() / write() asserts, through a shared run_work_test().
  5. -EDEADLK from a work_queue_run() worker is covered with self_free_worker(). With the #22268 change, -ESHUTDOWN is now tested too: a thread that calls work_queue_run() 100 ms after work_queue_free() started gets -ESHUTDOWN, and work_queue_free() waits for it.

The commit message is updated to list the cases. Passes on sim:ostest and sim:smp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants