Skip to content

Conversation

@kjyoun
Copy link
Contributor

@kjyoun kjyoun commented Sep 9, 2025

The code relies on a fundamental invariant: after pthread_join() returns, the joined thread is guaranteed to have terminated. This means no more callbacks will be made from that thread.

A failure in pthread_join() breaches this invariant and leads to an unexpected state where the supposedly-completed thread is still running and calling callbacks. This can lead to use-after-free errors or other race conditions. This change adds a check to enforce this invariant, preventing the application from continuing in a corrupt state.

#vibe-coded

Bug: 443334872

pthread_join is one of fundamental invariants in the code, ensuring that a thread is no longer running after it returns. Especially, no more callbacks are called from this point and after.  A failure in pthread_join breaches this invariant and leads to an state that code doesn't expect (e.g. the supposedly-completed thread is still calling the callbacks)
@kjyoun
Copy link
Contributor Author

kjyoun commented Sep 9, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly identifies and fixes a critical issue where the return value of pthread_join was not being checked. This could lead to serious problems like use-after-free errors and race conditions if a thread failed to join properly. The change to use SB_CHECK_EQ ensures that any failure in pthread_join will be caught, preventing the application from continuing in an unstable state. The changes are applied consistently across multiple files, and the minor update from NULL to nullptr is a good C++ modernization. Overall, this is an excellent and important improvement for the stability of the code.

@kjyoun kjyoun marked this pull request as ready for review September 9, 2025 18:19
@kjyoun kjyoun requested review from borongc and kaidokert September 9, 2025 18:19
@kjyoun kjyoun changed the title starboard: check pthread_join completes successfully starboard: Check pthread_join completes successfully Sep 9, 2025
Copy link
Member

@kaidokert kaidokert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me

@kjyoun kjyoun merged commit fb82fe8 into youtube:main Sep 9, 2025
401 of 446 checks passed
@kjyoun kjyoun deleted the pcm-crash-check branch September 9, 2025 23:15
@kjyoun kjyoun added the cp-26.android Cherry Pick to the 26.android branch label Sep 9, 2025
cobalt-github-releaser-bot pushed a commit that referenced this pull request Sep 9, 2025
The code relies on a fundamental invariant: after pthread_join()
returns, the joined thread is guaranteed to have terminated. This means
no more callbacks will be made from that thread.

A failure in `pthread_join()` breaches this invariant and leads to an
unexpected state where the supposedly-completed thread is still running
and calling callbacks. This can lead to use-after-free errors or other
race conditions. This change adds a check to enforce this invariant,
preventing the application from continuing in a corrupt state.

Bug: 443334872
(cherry picked from commit fb82fe8)

if (audio_out_thread_) {
pthread_join(*audio_out_thread_, nullptr);
SB_CHECK_EQ(pthread_join(*audio_out_thread_, nullptr), 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is correct and is what we should have implemented in the first place.

Just wanted to bring up that, if under certain situations the function returns a value other than 0, the app may continue to run with the old code, but will crash in production with the new code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crashing the app with a CHECK when pthread_join() fails is the intended behavior because it provides an immediate and clear stack trace.

The audio pipeline's destruction sequence relies on the guarantee that pthread_join() succeeds. Its success ensures the audio thread has terminated and will no longer issue callbacks. If this invariant is broken, a callback could be invoked during the destruction process, causing the app to crash in a corrupt state (see b/443334872#comment3).

Crashing immediately with a CHECK pinpoints the exact cause, preventing a more confusing crash later due to a race condition. This gives us a much cleaner picture of the problem at the earliest possible point.

kaidokert pushed a commit that referenced this pull request Sep 10, 2025
…fully (#7120)

Refer to the original PR: #7110

The code relies on a fundamental invariant: after pthread_join()
returns, the joined thread is guaranteed to have terminated. This means
no more callbacks will be made from that thread.

A failure in `pthread_join()` breaches this invariant and leads to an
unexpected state where the supposedly-completed thread is still running
and calling callbacks. This can lead to use-after-free errors or other
race conditions. This change adds a check to enforce this invariant,
preventing the application from continuing in a corrupt state.

#vibe-coded

Bug: 443334872

---------

Co-authored-by: Kyujung Youn <kjyoun@google.com>
github-actions bot pushed a commit that referenced this pull request Sep 10, 2025
The code relies on a fundamental invariant: after pthread_join()
returns, the joined thread is guaranteed to have terminated. This means
no more callbacks will be made from that thread.

A failure in `pthread_join()` breaches this invariant and leads to an
unexpected state where the supposedly-completed thread is still running
and calling callbacks. This can lead to use-after-free errors or other
race conditions. This change adds a check to enforce this invariant,
preventing the application from continuing in a corrupt state.

#vibe-coded

Bug: 443334872

original-hexsha: fb82fe8
jammel-yeboah pushed a commit to jammel-yeboah/cobalt that referenced this pull request Oct 2, 2025
The code relies on a fundamental invariant: after pthread_join()
returns, the joined thread is guaranteed to have terminated. This means
no more callbacks will be made from that thread.

A failure in `pthread_join()` breaches this invariant and leads to an
unexpected state where the supposedly-completed thread is still running
and calling callbacks. This can lead to use-after-free errors or other
race conditions. This change adds a check to enforce this invariant,
preventing the application from continuing in a corrupt state.

#vibe-coded

Bug: 443334872
andrewsavage1 pushed a commit that referenced this pull request Oct 9, 2025
The code relies on a fundamental invariant: after pthread_join()
returns, the joined thread is guaranteed to have terminated. This means
no more callbacks will be made from that thread.

A failure in `pthread_join()` breaches this invariant and leads to an
unexpected state where the supposedly-completed thread is still running
and calling callbacks. This can lead to use-after-free errors or other
race conditions. This change adds a check to enforce this invariant,
preventing the application from continuing in a corrupt state.

#vibe-coded

Bug: 443334872

original-hexsha: fb82fe8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cp-26.android Cherry Pick to the 26.android branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants