Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible crash on program exit with libnice #1345

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions src/impl/icetransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,22 @@ void IceTransport::LogCallback(juice_log_level_t level, const char *message) {

#else // USE_NICE == 1

unique_ptr<GMainLoop, void (*)(GMainLoop *)> IceTransport::MainLoop(nullptr, nullptr);
std::thread IceTransport::MainLoopThread;
IceTransport::MainLoopWrapper *IceTransport::MainLoop = nullptr;

IceTransport::MainLoopWrapper::MainLoopWrapper()
: mMainLoop(g_main_loop_new(nullptr, FALSE), g_main_loop_unref) {
if (!mMainLoop)
throw std::runtime_error("Failed to create the glib main loop");

mThread = std::thread(g_main_loop_run, mMainLoop.get());
}

IceTransport::MainLoopWrapper::~MainLoopWrapper() {
g_main_loop_quit(mMainLoop.get());
mThread.join();
}

GMainLoop *IceTransport::MainLoopWrapper::get() const { return mMainLoop.get(); }

void IceTransport::Init() {
g_log_set_handler("libnice", G_LOG_LEVEL_MASK, LogCallback, nullptr);
Expand All @@ -402,17 +416,12 @@ void IceTransport::Init() {
nice_debug_enable(false); // do not output STUN debug messages
}

MainLoop = decltype(MainLoop)(g_main_loop_new(nullptr, FALSE), g_main_loop_unref);
if (!MainLoop)
throw std::runtime_error("Failed to create the main loop");

MainLoopThread = std::thread(g_main_loop_run, MainLoop.get());
MainLoop = new MainLoopWrapper;
}

void IceTransport::Cleanup() {
g_main_loop_quit(MainLoop.get());
MainLoopThread.join();
MainLoop.reset();
delete MainLoop;
MainLoop = nullptr;
}

static void closeNiceAgentCallback(GObject *niceAgent, GAsyncResult *, gpointer) {
Expand Down Expand Up @@ -447,7 +456,7 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi
// Create agent
mNiceAgent = decltype(mNiceAgent)(
nice_agent_new_full(
g_main_loop_get_context(MainLoop.get()),
g_main_loop_get_context(MainLoop->get()),
NICE_COMPATIBILITY_RFC5245, // RFC 5245 was obsoleted by RFC 8445 but this should be OK
flags),
closeNiceAgent);
Expand Down Expand Up @@ -580,12 +589,13 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi
nice_agent_set_port_range(mNiceAgent.get(), mStreamId, 1, config.portRangeBegin,
config.portRangeEnd);

nice_agent_attach_recv(mNiceAgent.get(), mStreamId, 1, g_main_loop_get_context(MainLoop.get()),
nice_agent_attach_recv(mNiceAgent.get(), mStreamId, 1, g_main_loop_get_context(MainLoop->get()),
RecvCallback, this);
}

void IceTransport::setIceAttributes([[maybe_unused]] string uFrag, [[maybe_unused]] string pwd) {
PLOG_WARNING << "Setting custom ICE attributes is not supported with libnice, please use libjuice";
PLOG_WARNING
<< "Setting custom ICE attributes is not supported with libnice, please use libjuice";
}

void IceTransport::addIceServer(IceServer server) {
Expand Down Expand Up @@ -647,7 +657,7 @@ void IceTransport::addIceServer(IceServer server) {

IceTransport::~IceTransport() {
PLOG_DEBUG << "Destroying ICE transport";
nice_agent_attach_recv(mNiceAgent.get(), mStreamId, 1, g_main_loop_get_context(MainLoop.get()),
nice_agent_attach_recv(mNiceAgent.get(), mStreamId, 1, g_main_loop_get_context(MainLoop->get()),
NULL, NULL);
nice_agent_remove_stream(mNiceAgent.get(), mStreamId);
mNiceAgent.reset();
Expand Down
13 changes: 11 additions & 2 deletions src/impl/icetransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,17 @@ class IceTransport : public Transport {
static void RecvCallback(juice_agent_t *agent, const char *data, size_t size, void *user_ptr);
static void LogCallback(juice_log_level_t level, const char *message);
#else
static unique_ptr<GMainLoop, void (*)(GMainLoop *)> MainLoop;
static std::thread MainLoopThread;
class MainLoopWrapper {
public:
MainLoopWrapper();
~MainLoopWrapper();
GMainLoop *get() const;

private:
unique_ptr<GMainLoop, void (*)(GMainLoop *)> mMainLoop;
std::thread mThread;
};
static MainLoopWrapper *MainLoop;

unique_ptr<NiceAgent, void (*)(NiceAgent *)> mNiceAgent;
uint32_t mStreamId = 0;
Expand Down
Loading