Skip to content

Setting audioOn before the audio threads start - #104

Merged
wberube merged 1 commit into
OpenIPC:masterfrom
erkin-io:fix/audio-start-race
Nov 21, 2026
Merged

wberube merged 1 commit into
OpenIPC:masterfrom
erkin-io:fix/audio-start-race

Conversation

@erkin-io

Copy link
Copy Markdown
Contributor

What was wrong

Audio can stop right after it starts, with no error. The capture thread (aud_thread, from each HAL) and the encoding thread (aenc_thread()) both loop on while (keepRunning && audioOn). media_audio_enable() sets audioOn = 1 only after it has created both threads. If a new thread runs before the creating thread reaches that line, it sees audioOn == 0 and returns at once.

It depends on the scheduler which thread runs first, so it may never show on one system and always show on another. On a Fullhan FH8856V200 board, it never showed with the vendor kernel (4.9). With a newer kernel (Linux 6.18), the audio threads ended right after start every time.

Why

audioOn is the threads' run condition, so it has to be set before they start. It is set at the end of media_audio_enable(), after both pthread_create() calls.

What this changes

  • src/media.c, media_audio_enable(): audioOn = 1 moves to before the two pthread_create() calls.
  • Because the flag is now set first, a failed pthread_create() has to undo it. audioOn goes back to 0. If the capture thread was already running, it is joined. The attribute object is released before the error return. Otherwise audioOn would stay 1 after a failure: a later media_audio_enable() would return at once, and media_audio_disable() would join a thread that never existed.

How it was checked

  • Built with ./build.sh fhan-v2, no new warnings. ./test.sh -a passes.
  • On a Ruision F8 board (FH8856V200, GC4653) running Linux 6.18, inside our firmware:
    • Before: audio stopped at once after every start.
    • After: audio runs, checked with RTSP recordings and an audio/video sync test.
  • On the vendor kernel the change has no visible effect, as expected.
  • The failure path (a thread that cannot be created) was compiled and read, not provoked.

Effect on other platforms

Shared code, every target with audio. Every HAL's capture thread loops on audioOn the same way: HiSilicon, Ingenic, Anyka, Fullhan and the others. Where the race never showed, nothing changes.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

Start audio threads only after setting their run flag

🐞 Bug fix 🕐 10-20 Minutes

Grey Divider

AI Description

• Set audioOn before creating audio threads so they cannot exit immediately on startup.
• Reset the flag on thread-creation failure and join an already-started capture thread.
Merge Risk 🟡 Medium

Blast radius 🟢 Low 💥 1 capability

  • 1 affected end-to-end capability after fault-specific containment; implementation boundaries do not widen its functional scope.
  • Capture, MP3 encoding, PCM/MP3 delivery, RTSP/RTMP ingestion, and recording consume the same audio worker pipeline.
  • The HAL thread is an implementation hop into this single end-to-end audio behavior, not an independent functional scope; its loop is controlled by the changed audioOn lifecycle.
Behavioral complexity 🔴 High 🧩 Interacting branches, Concurrent ordering

  • Before: audioOn was assigned after both pthread_create calls, so a newly started worker could observe false and exit; failure cleanup was not coordinated with a pre-enabled lifecycle state.
  • After: audioOn is assigned before worker creation, and each creation failure changes the shared state, joins or does not join the other worker, and destroys thread attributes.
  • New correctness condition: the two-worker startup protocol must preserve a consistent state across successful creation, capture-create failure, encoder-create failure, worker exit, resource cleanup, and later disable/re-enable calls.
  • The changed ordering is concurrent behavior, while the new failure branches introduce interacting partial-start states.
  • The source also shows an unresolved invalid-attribute lifecycle because the new branches destroy thread_attr before the surrounding cleanup code uses it again.
Potential damage 🟡 Medium 🩹 Temporary loss

  • A worker-creation failure can make the complete audio media pipeline unavailable while media_audio_enable continues without a failure result.
  • Reverting the code does not repair the already running process's leaked/partially initialized state.
  • Recovery requires a process restart or an externally supplied cleanup/reinitialization path; the existing media_audio_disable path is not reachable for this state because it returns when audioOn is false.
  • The damage is limited to the audio capability rather than unrelated video functions.
  • Recoverable after process restart or verified manual platform/audio cleanup followed by reinitialization.
  • No automatic repair or retry invocation is established; configuration rollback alone does not release the leaked ring buffer, encoder, HAL state, or joinable worker.
Assessed commit 1f83e2b
Diagram

graph TD
  Enable["Enable audio"] --> Flag["Set audioOn"] --> Capture["Start capture"] --> CaptureOK{"Capture started?"}
  CaptureOK -- Yes --> Encoder["Start encoder"] --> EncoderOK{"Encoder started?"}
  CaptureOK -- No --> Rollback["Reset audioOn"]
  EncoderOK -- No, join capture --> Rollback
  EncoderOK -- Yes --> Active["Audio running"]
Loading
High-Level Assessment

Setting the existing run flag before thread creation directly fixes the startup race without adding synchronization machinery. Resetting it and joining capture when encoder creation fails keeps the thread state consistent with the flag; a startup barrier would add complexity without a clear benefit here.

Files changed (1) +16 / -4

Bug fix (1) +16 / -4
media.cSet the audio run flag before starting worker threads +16/-4

Set the audio run flag before starting worker threads

• Moves 'audioOn = 1' ahead of capture and encoder thread creation so neither worker can observe the previous inactive state at startup. On thread-creation failure, resets the flag and destroys the thread attributes; if encoder creation fails, also joins the capture thread.

src/media.c

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can add REVIEW.md to your repo root and Qodo follows it on every PR

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@wberube
wberube merged commit 1bb2d6d into OpenIPC:master Nov 21, 2026
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.

2 participants