Skip to content

Commit

Permalink
Refactor MediaRouteApplication to avoid deadlock in NotifyStreamPrepa…
Browse files Browse the repository at this point in the history
…red and NotifyStreamDeleted
  • Loading branch information
getroot committed Aug 7, 2024
1 parent 6fb5875 commit 9844959
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/projects/mediarouter/mediarouter_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,12 @@ bool MediaRouteApplication::NotifyStreamCreate(const std::shared_ptr<info::Strea
bool MediaRouteApplication::NotifyStreamPrepared(std::shared_ptr<MediaRouteStream> &stream)
{
std::shared_lock<std::shared_mutex> lock(_observers_lock);
auto observers = _observers; // Avoid deadlock
lock.unlock();

logti("[%s/%s(%u)] Stream has been prepared %s", _application_info.GetName().CStr(), stream->GetStream()->GetName().CStr(), stream->GetStream()->GetId(), stream->GetStream()->GetInfoString().CStr());

for (auto observer : _observers)
for (auto observer : observers)
{
auto observer_type = observer->GetObserverType();

Expand Down Expand Up @@ -628,10 +630,12 @@ bool MediaRouteApplication::DeleteOutboundStream(const std::shared_ptr<info::Str
bool MediaRouteApplication::NotifyStreamDeleted(const std::shared_ptr<info::Stream> &stream_info, const MediaRouterApplicationConnector::ConnectorType connector_type)
{
std::shared_lock<std::shared_mutex> lock_guard(_observers_lock);
auto observers = _observers; // Avoid deadlock
lock_guard.unlock();

auto representation_type = stream_info->GetRepresentationType();

for (auto it = _observers.begin(); it != _observers.end(); ++it)
for (auto it = observers.begin(); it != observers.end(); ++it)
{
auto observer = *it;

Expand Down Expand Up @@ -672,10 +676,12 @@ bool MediaRouteApplication::NotifyStreamDeleted(const std::shared_ptr<info::Stre
bool MediaRouteApplication::NotifyStreamUpdated(const std::shared_ptr<info::Stream> &stream_info, const MediaRouterApplicationConnector::ConnectorType connector_type)
{
std::shared_lock<std::shared_mutex> lock_guard(_observers_lock);
auto observers = _observers; // Avoid deadlock
lock_guard.unlock();

auto representation_type = stream_info->GetRepresentationType();

for (auto it = _observers.begin(); it != _observers.end(); ++it)
for (auto it = observers.begin(); it != observers.end(); ++it)
{
auto observer = *it;

Expand Down Expand Up @@ -875,7 +881,9 @@ void MediaRouteApplication::InboundWorkerThread(uint32_t worker_id)
}

std::shared_lock<std::shared_mutex> lock(_observers_lock);
for (const auto &observer : _observers)
auto observers = _observers; // Avoid deadlock
lock.unlock();
for (const auto &observer : observers)
{
auto observer_type = observer->GetObserverType();

Expand Down Expand Up @@ -958,7 +966,9 @@ void MediaRouteApplication::OutboundWorkerThread(uint32_t worker_id)
}

std::shared_lock<std::shared_mutex> lock(_observers_lock);
for (const auto &observer : _observers)
auto observers = _observers; // Avoid deadlock
lock.unlock();
for (const auto &observer : observers)
{
auto observer_type = observer->GetObserverType();

Expand Down

0 comments on commit 9844959

Please sign in to comment.