Skip to content

Commit

Permalink
Catch exceptions from user callbacks early
Browse files Browse the repository at this point in the history
  • Loading branch information
paullouisageneau committed Feb 1, 2023
1 parent cb2f297 commit 11b09b6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
47 changes: 39 additions & 8 deletions src/impl/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,58 @@
*/

#include "channel.hpp"
#include "internals.hpp"

namespace rtc::impl {

void Channel::triggerOpen() {
mOpenTriggered = true;
openCallback();
try {
openCallback();
} catch (const std::exception &e) {
PLOG_WARNING << "Uncaught exception in callback: " << e.what();
}
flushPendingMessages();
}

void Channel::triggerClosed() { closedCallback(); }
void Channel::triggerClosed() {
try {
closedCallback();
} catch (const std::exception &e) {
PLOG_WARNING << "Uncaught exception in callback: " << e.what();
}
}

void Channel::triggerError(string error) { errorCallback(std::move(error)); }
void Channel::triggerError(string error) {
try {
errorCallback(std::move(error));
} catch (const std::exception &e) {
PLOG_WARNING << "Uncaught exception in callback: " << e.what();
}
}

void Channel::triggerAvailable(size_t count) {
if (count == 1)
availableCallback();
if (count == 1) {
try {
availableCallback();
} catch (const std::exception &e) {
PLOG_WARNING << "Uncaught exception in callback: " << e.what();
}
}

flushPendingMessages();
}

void Channel::triggerBufferedAmount(size_t amount) {
size_t previous = bufferedAmount.exchange(amount);
size_t threshold = bufferedAmountLowThreshold.load();
if (previous > threshold && amount <= threshold)
bufferedAmountLowCallback();
if (previous > threshold && amount <= threshold) {
try {
bufferedAmountLowCallback();
} catch (const std::exception &e) {
PLOG_WARNING << "Uncaught exception in callback: " << e.what();
}
}
}

void Channel::flushPendingMessages() {
Expand All @@ -43,7 +70,11 @@ void Channel::flushPendingMessages() {
if (!next)
break;

messageCallback(*next);
try {
messageCallback(*next);
} catch (const std::exception &e) {
PLOG_WARNING << "Uncaught exception in callback: " << e.what();
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/impl/peerconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,13 @@ void PeerConnection::triggerPendingDataChannels() {
break;

auto impl = std::move(*next);
dataChannelCallback(std::make_shared<rtc::DataChannel>(impl));

try {
dataChannelCallback(std::make_shared<rtc::DataChannel>(impl));
} catch (const std::exception &e) {
PLOG_WARNING << "Uncaught exception in callback: " << e.what();
}

impl->triggerOpen();
}
}
Expand All @@ -1106,7 +1112,13 @@ void PeerConnection::triggerPendingTracks() {
break;

auto impl = std::move(*next);
trackCallback(std::make_shared<rtc::Track>(impl));

try {
trackCallback(std::make_shared<rtc::Track>(impl));
} catch (const std::exception &e) {
PLOG_WARNING << "Uncaught exception in callback: " << e.what();
}

// Do not trigger open immediately for tracks as it'll be done later
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/impl/peerconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {

// Helper method for asynchronous callback invocation
template <typename... Args> void trigger(synchronized_callback<Args...> *cb, Args... args) {
(*cb)(std::move(args...));
try {
(*cb)(std::move(args...));
} catch (const std::exception &e) {
PLOG_WARNING << "Uncaught exception in callback: " << e.what();
}
}

const Configuration config;
Expand Down

0 comments on commit 11b09b6

Please sign in to comment.