From 933364a72211178d89ff852289f5ff81fe7a08f0 Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Wed, 23 Oct 2024 12:33:33 +0200 Subject: [PATCH] Catch uncaught exceptions from incoming media handler chain --- src/impl/peerconnection.cpp | 15 ++++++++++----- src/impl/track.cpp | 19 +++++++++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/impl/peerconnection.cpp b/src/impl/peerconnection.cpp index a6515446e..58e569e6e 100644 --- a/src/impl/peerconnection.cpp +++ b/src/impl/peerconnection.cpp @@ -538,11 +538,16 @@ void PeerConnection::forwardMedia([[maybe_unused]] message_ptr message) { if (auto handler = getMediaHandler()) { message_vector messages{std::move(message)}; - handler->incoming(messages, [this](message_ptr message) { - auto transport = std::atomic_load(&mDtlsTransport); - if (auto srtpTransport = std::dynamic_pointer_cast(transport)) - srtpTransport->send(std::move(message)); - }); + try { + handler->incomingChain(messages, [this](message_ptr message) { + auto transport = std::atomic_load(&mDtlsTransport); + if (auto srtpTransport = std::dynamic_pointer_cast(transport)) + srtpTransport->send(std::move(message)); + }); + } catch(const std::exception &e) { + PLOG_WARNING << "Exception in global incoming media handler: " << e.what(); + return; + } for (auto &m : messages) dispatchMedia(std::move(m)); diff --git a/src/impl/track.cpp b/src/impl/track.cpp index 8bbbe778a..6ff61ad4b 100644 --- a/src/impl/track.cpp +++ b/src/impl/track.cpp @@ -141,12 +141,18 @@ void Track::incoming(message_ptr message) { } message_vector messages{std::move(message)}; - if (auto handler = getMediaHandler()) - handler->incomingChain(messages, [this, weak_this = weak_from_this()](message_ptr m) { - if (auto locked = weak_this.lock()) { - transportSend(m); - } - }); + if (auto handler = getMediaHandler()) { + try { + handler->incomingChain(messages, [this, weak_this = weak_from_this()](message_ptr m) { + if (auto locked = weak_this.lock()) { + transportSend(m); + } + }); + } catch (const std::exception &e) { + PLOG_WARNING << "Exception in incoming media handler: " << e.what(); + return; + } + } for (auto &m : messages) { // Tail drop if queue is full @@ -184,6 +190,7 @@ bool Track::outgoing(message_ptr message) { transportSend(m); } }); + bool ret = false; for (auto &m : messages) ret = transportSend(std::move(m));