Skip to content

fix(smoke): stop the C client returning from main while libmoq holds ctx - #2675

Merged
kixelated merged 1 commit into
mainfrom
claude/inspiring-sutherland-fec3f2
Aug 5, 2026
Merged

kixelated merged 1 commit into
mainfrom
claude/inspiring-sutherland-fec3f2

Conversation

@kixelated

Copy link
Copy Markdown
Collaborator

Root cause

test/smoke/clients/c/subscribe.c registers &c (a ctx_t on main's stack) with four libmoq callbacks: on_status via moq_session_connect, on_broadcast via moq_origin_consume_announced, on_catalog via moq_consume_catalog, and on_frame via moq_consume_video. Every one of those documents the same contract:

The caller must keep user_data valid until the terminal (<= 0) callback.

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 1 from main, which destroys the frame &c points 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, and timed 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 keeps main'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 the OnStatus wrapper without calling it), so nothing holds &c yet.

Also updates the stale comment to describe what the code does today, per CLAUDE.md.

Notes

  • No behavior change on the success path.
  • moq-dev/smoke's clients/c/subscribe.c is 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 the openh264/vaapi/cuda -> openh264/cuda edit from fix(libmoq): unbreak the Linux C link list, and make VAAPI opt-in #2669.
  • The _exit workaround itself masks an unexplained abort inside libmoq (glibc's __pthread_tpp_change_priority priority-protected-mutex assertion) that has never been root-caused. Filed separately rather than papered over further here.

Testing

nix develop --command just check passes. The C file compiles clean under -Wall -Wextra against the generated moq.h.

(written by Opus 5)

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

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3bccf5eb-72ca-4c5a-927c-0d5e1a65afae

📥 Commits

Reviewing files that changed from the base of the PR and between 270bded and 5938ae6.

📒 Files selected for processing (1)
  • test/smoke/clients/c/subscribe.c

Walkthrough

The C subscribe client adds a variadic fail helper that formats diagnostics, flushes output, and exits with status 1. After callback registration begins, failure and success paths use _exit so main does not return while callback context remains active. Session-connect failure continues to return normally because it does not retain callback context.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: preventing the C client from returning while libmoq callbacks reference the stack-allocated context.
Description check ✅ Passed The description is directly related to the changeset, explaining the root cause, the fix implementation, and the rationale for the changes made.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch claude/inspiring-sutherland-fec3f2

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kixelated
kixelated merged commit f879cd2 into main Aug 5, 2026
2 checks passed
@kixelated
kixelated deleted the claude/inspiring-sutherland-fec3f2 branch August 5, 2026 17:41
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant