Skip to content

fix: protect _transcode_apps with shared_mutex in Transcoder#2067

Merged
getroot merged 1 commit intomasterfrom
fix/transcoder-app-map-mutex
Apr 13, 2026
Merged

fix: protect _transcode_apps with shared_mutex in Transcoder#2067
getroot merged 1 commit intomasterfrom
fix/transcoder-app-map-mutex

Conversation

@getroot
Copy link
Copy Markdown
Member

@getroot getroot commented Apr 13, 2026

Problem

Transcoder::_transcode_apps (std::map) was accessed from multiple threads without synchronization:

  • Write: OnCreateApplication, OnDeleteApplication — called from the MediaRouter thread
  • Read: GetApplicationById — can be called from API server threads or other contexts

This is a data race on std::map, which is undefined behavior and can cause crashes or corruption.

Changes

  • Add mutable std::shared_mutex _transcode_apps_mutex to Transcoder
  • unique_lock for all write paths (OnCreateApplication, OnDeleteApplication)
  • shared_lock for read paths (GetApplicationById)
  • OnDeleteApplication now extracts the shared_ptr under the lock, then calls UnregisterObserverApp/UnregisterConnectorApp outside the lock to avoid lock inversion with MediaRouter
  • OnCreateApplication rolls back the map insertion if MediaRouter registration fails

OnCreateApplication/OnDeleteApplication are called from the MediaRouter
thread while GetApplicationById can be called from the API server thread.
Without synchronization this is a data race on std::map.

- Add mutable std::shared_mutex _transcode_apps_mutex to Transcoder
- unique_lock for writes (Create/Delete)
- shared_lock for reads (GetApplicationById)
- OnDeleteApplication now extracts the shared_ptr under the lock then
  unregisters from MediaRouter outside the lock to avoid lock inversion
@getroot getroot requested a review from a team as a code owner April 13, 2026 08:55
@getroot getroot requested review from dimiden and removed request for a team April 13, 2026 08:55
Copy link
Copy Markdown
Member

@dimiden dimiden left a comment

Choose a reason for hiding this comment

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

LGTM

@getroot getroot merged commit e5b90ce into master Apr 13, 2026
2 checks passed
@getroot getroot deleted the fix/transcoder-app-map-mutex branch April 13, 2026 09:59
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