Repository navigation
fix(smoke): stop the C client returning from main while libmoq holds ctx - #2675
Merged
Merged
Conversation
libmoq's docs for moq_origin_consume_announced, moq_consume_catalog and moq_consume_video all require user_data stay valid until the terminal (<= 0) callback fires. The C smoke client registers &c (main's stack frame) with all three plus on_status, then never waits for any of those terminal callbacks, so every `return 1` past connect hands libmoq's runtime thread a pointer into a dead frame. Route those failures through a fail() helper that _exits instead. That fixes the use-after-free and makes the exit symmetric with the success path, which has used _exit(0) since 17bf0d9 to dodge an abort in the atexit teardown. A failure that aborts with SIGABRT instead of printing "timed out waiting for data" and exiting 1 is the worst place to lose the message. The paths before connect still return: a registration that fails never invokes its callback, so nothing holds &c yet. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Contributor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
WalkthroughThe C subscribe client adds a variadic 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches✨ Simplify code
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
kixelated
added a commit
that referenced
this pull request
Sep 3, 2026
The `rust -> c` leg was flaky, and 17bf0d9 papered over it with `_exit(0)` on the success path, blaming a glibc priority-protected-mutex assertion on openh264's worker threads at atexit. That diagnosis was wrong: the subscribe client never encodes, so no openh264 thread exists, and libmoq's runtime thread is harmless at exit. The real mechanism is a use-after-return of `ctx_t`. It lives on main's stack and is handed to four separate registrations as `user_data`. `moq_session_close` ends the session's status registration alone; `moq_origin_consume_announced`, `moq_consume_catalog` and `moq_consume_video` each keep the pointer until their own terminal (<= 0) callback. The old code closed the session and returned immediately, so `exit()`'s call frames landed on the same addresses `ctx` had occupied while the runtime thread was still calling `on_frame`, which locks `ctx->mu` and broadcasts `ctx->cv` right over the live exit path. Reproduced on Linux/glibc 2.36 (aarch64) with a relay, an ffmpeg H.264 publisher and the pre-17bf0d950 shape: 4 fatal signals in 100 runs. Under gdb the faulting process shows the `libmoq` thread inside `pthread_cond_broadcast(&ctx->cv)` from `on_frame`, on a condvar 16 bytes above main thread's live `sp`, while the main thread's PC is garbage. Whichever field the exit path clobbers first picks the symptom: a corrupted return address gives SIGSEGV/SIGBUS, a corrupted `pthread_mutex_t.__kind` routes an ordinary lock down glibc's TPP branch and gives the reported `__pthread_tpp_change_priority` assertion. So the success path now closes all four registrations, waits for all four terminals, and returns. That drain is also a standing assertion: a terminal callback that stops firing fails the leg deterministically instead of leaving a 4% flake. The failure paths keep `_exit` for the `user_data` lifetime reason from #2675. Measured on the same host, 200 runs each: pre-fix stack `ctx` 4/100 fatal; drained `ctx` 0/200; no registrations at all 0/200; the fixed client 0/200. `cpp/obs` was never affected: `moq_source_destroy` already refcounts every subscription and waits for the terminals. Closes #2676 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
kixelated
added a commit
that referenced
this pull request
Sep 4, 2026
The `rust -> c` leg was flaky, and 17bf0d9 papered over it with `_exit(0)` on the success path, blaming a glibc priority-protected-mutex assertion on openh264's worker threads at atexit. That diagnosis was wrong: the subscribe client never encodes, so no openh264 thread exists, and libmoq's runtime thread is harmless at exit. The real mechanism is a use-after-return of `ctx_t`. It lives on main's stack and is handed to four separate registrations as `user_data`. `moq_session_close` ends the session's status registration alone; `moq_origin_consume_announced`, `moq_consume_catalog` and `moq_consume_video` each keep the pointer until their own terminal (<= 0) callback. The old code closed the session and returned immediately, so `exit()`'s call frames landed on the same addresses `ctx` had occupied while the runtime thread was still calling `on_frame`, which locks `ctx->mu` and broadcasts `ctx->cv` right over the live exit path. Reproduced on Linux/glibc 2.36 (aarch64) with a relay, an ffmpeg H.264 publisher and the pre-17bf0d950 shape: 4 fatal signals in 100 runs. Under gdb the faulting process shows the `libmoq` thread inside `pthread_cond_broadcast(&ctx->cv)` from `on_frame`, on a condvar 16 bytes above main thread's live `sp`, while the main thread's PC is garbage. Whichever field the exit path clobbers first picks the symptom: a corrupted return address gives SIGSEGV/SIGBUS, a corrupted `pthread_mutex_t.__kind` routes an ordinary lock down glibc's TPP branch and gives the reported `__pthread_tpp_change_priority` assertion. So the success path now closes all four registrations, waits for all four terminals, and returns. That drain is also a standing assertion: a terminal callback that stops firing fails the leg deterministically instead of leaving a 4% flake. The failure paths keep `_exit` for the `user_data` lifetime reason from #2675. Measured on the same host, 200 runs each: pre-fix stack `ctx` 4/100 fatal; drained `ctx` 0/200; no registrations at all 0/200; the fixed client 0/200. `cpp/obs` was never affected: `moq_source_destroy` already refcounts every subscription and waits for the terminals. Closes #2676 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
test/smoke/clients/c/subscribe.cregisters&c(actx_tonmain's stack) with four libmoq callbacks:on_statusviamoq_session_connect,on_broadcastviamoq_origin_consume_announced,on_catalogviamoq_consume_catalog, andon_frameviamoq_consume_video. Every one of those documents the same contract:The client never waits for any of those terminal callbacks. It waits on a condvar for a frame, and on failure did a plain
return 1frommain, which destroys the frame&cpoints into while libmoq's runtime thread is still free to call back into it. The stale comment at the top of the file asserted the opposite ("main blocks until got/timeout, then the process exits, so there's no use-after-free"), which was true only of the success path.Four paths were affected:
moq_origin_consume_announced failed,broadcast never announced,moq_consume_catalog failed, andtimed out waiting for data.Fix
Route the post-connect failures through a
_Noreturn fail()helper that prints the same message, flushes, and_exit(1)s. That keepsmain's frame alive for as long as the callbacks can run, and it makes the exit symmetric with the success path, which has used_exit(0)since 17bf0d9 to skip an abort in the atexit teardown. Those failure paths were still running that teardown, so the case where a legible message matters most (a timeout, i.e. the smoke test actually failing) was the case most likely to be replaced by a SIGABRT/134.The paths before connect keep their plain
return 1: a registration that fails never invokes its callback (libmoq drops theOnStatuswrapper without calling it), so nothing holds&cyet.Also updates the stale comment to describe what the code does today, per CLAUDE.md.
Notes
moq-dev/smoke'sclients/c/subscribe.cis a copy of this file (differing only in the first comment line, which names the prebuilt libmoq instead of the workspace one). Updated to match; that repo's copy had also missed theopenh264/vaapi/cuda->openh264/cudaedit from fix(libmoq): unbreak the Linux C link list, and make VAAPI opt-in #2669._exitworkaround itself masks an unexplained abort inside libmoq (glibc's__pthread_tpp_change_prioritypriority-protected-mutex assertion) that has never been root-caused. Filed separately rather than papered over further here.Testing
nix develop --command just checkpasses. The C file compiles clean under-Wall -Wextraagainst the generatedmoq.h.(written by Opus 5)