fix(lib): stop cbuf_put/get orphaning their mutex on cancellation, hanging vtaskmgr_fini

pthread_cond_wait() reacquires its mutex before honoring a cancellation,
but mylock.c installed no cleanup handler to release it again -- a
thread cancelled mid-wait terminated holding the lock forever. vtaskmgr's
pthread_cancel()-based teardown could land there from three angles
(dispatcher cancelled while polling the shared queue, or a vcpu racing
back into it after its own shutdown signal), permanently deadlocking any
other thread that touched the same queue and hanging vtaskmgr_fini()'s
pthread_join() indefinitely.

Reproduced live as xce_test hanging on Ctrl-D exit once a connection
context was entered; gdb confirmed a vcpu thread blocked forever on a
mutex owned by an already-exited thread ID.

Fix: cbuf_put()/cbuf_get()/cbuf_timedget() now unlock via a
pthread_cleanup_push/pop handler around the wait, so a cancelled waiter
always releases the mutex. vcpu_idle()/vcpu_entry() also recognize the
teardown poison pill and exit immediately instead of needlessly
re-announcing into the already-dead shared queue.

New test_vtaskmgr_teardown.c reproduces the race deterministically
(self-pinned to one CPU, since true multi-core parallelism rarely hits
the window) and bounds vtaskmgr_fini() with a timed join so a
regression fails fast instead of hanging the suite. Logged as bug-1810.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RLSNJhNoULvVcUhXEFafQC
This commit is contained in:
Rongsong Shen 2026-09-15 04:13:53 +00:00
parent b7250c2ec6
commit 0f3919f195
No known key found for this signature in database
GPG key ID: 5BCDAAE0123E9FE2
9 changed files with 335 additions and 9 deletions

View file

@ -4721,3 +4721,26 @@
| 16:30 | Edited src/lib/utils.c | modified hash_string() | ~24 |
| 19:34 | Edited src/include/utils.h | expanded (+6 lines) | ~113 |
| 19:34 | Edited src/scheduler/src/core_events.c | DEBUG() → DBG() | ~325 |
| 19:46 | Session end: 8 writes across 3 files (utils.h, core_events.c, utils.c) | 2 reads | ~14326 tok |
## Session: 2026-09-15 19:46
| Time | Action | File(s) | Outcome | ~Tokens |
|------|--------|---------|---------|--------|
| 20:00 | Created src/lib/testsuite/test_vtaskmgr_teardown.c | — | ~1070 |
| 20:01 | Edited src/lib/testsuite/test.c | 2→3 lines | ~32 |
| 20:01 | Edited src/lib/meson.build | 2→3 lines | ~44 |
| 20:02 | Edited src/lib/testsuite/test.c | 3→4 lines | ~33 |
| 20:09 | Edited src/lib/testsuite/test_vtaskmgr_teardown.c | 2→2 lines | ~17 |
| 20:12 | Edited src/lib/testsuite/test_vtaskmgr_teardown.c | modified struct() | ~489 |
| 20:24 | Edited src/lib/testsuite/test_vtaskmgr_teardown.c | expanded (+10 lines) | ~296 |
| 20:24 | Edited src/lib/testsuite/test_vtaskmgr_teardown.c | modified test_vtaskmgr_fini_idle_multi_vcpu_does_not_hang() | ~214 |
| 20:24 | Edited src/lib/testsuite/test_vtaskmgr_teardown.c | added 1 condition(s) | ~47 |
| 20:28 | Edited src/lib/vtaskmgr.c | added 1 condition(s) | ~292 |
| 20:28 | Edited src/lib/vtaskmgr.c | added 1 condition(s) | ~265 |
| 20:37 | Edited src/lib/mylock.c | modified while() | ~255 |
| 20:37 | Edited src/lib/mylock.c | modified while() | ~188 |
| 20:38 | Edited src/lib/mylock.c | modified while() | ~185 |
| 20:42 | Edited src/lib/mylock.c | added 1 condition(s) | ~303 |
| 20:56 | Edited src/lib/vtaskmgr.c | 11→13 lines | ~236 |
| 20:56 | Edited src/lib/vtaskmgr.c | 5→4 lines | ~74 |