Skip to content

Commit 7f63585

Browse files
Fix use-after-free freeze in Linux TaskRunnerLinux::EnqueueTask (#1177)
On Linux each AudioRendererSink / VisualizerSink owns a TaskRunnerLinux that posts to the GLib main loop with a raw this pointer. stopAudioRenderer / stopVisualizer destroy the sink synchronously, so any idle callback still pending is dispatched against freed memory and locks a mutex that no longer exists, which hangs the GTK thread permanently. The runner is now held via shared_ptr and the idle callback receives a heap-allocated weak_ptr through g_main_context_invoke_full, freed by the destroy notify. If the runner is gone by dispatch time the callback no-ops. Queued tasks also run outside tasks_mutex_ so a re-entrant EnqueueTask cannot deadlock. Verified with an AddressSanitizer harness driving the runner the way the sink does: pre-fix 0/20 runs survive, post-fix 20/20.
1 parent b0e5db2 commit 7f63585

4 files changed

Lines changed: 50 additions & 18 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
patch type="fixed" "Fix use-after-free crash in TaskRunnerLinux::EnqueueTask when the runner is destroyed before the main loop dispatches a queued task"

‎linux/livekit_plugin.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class VisualizerSink : public libwebrtc::AudioTrackSink {
7373
&flutter::StandardMethodCodec::GetInstance())),
7474
media_track_(media_track), is_centered_(is_centered),
7575
bar_count_(bar_count) {
76-
task_runner_ = std::make_unique<livekit_client_plugin::TaskRunnerLinux>();
76+
task_runner_ = std::make_shared<livekit_client_plugin::TaskRunnerLinux>();
7777
auto handler = std::make_unique<
7878
flutter::StreamHandlerFunctions<flutter::EncodableValue>>(
7979
[&](const flutter::EncodableValue *arguments,
@@ -151,7 +151,7 @@ class VisualizerSink : public libwebrtc::AudioTrackSink {
151151

152152
private:
153153
std::unique_ptr<AudioVisualizer> audio_visualizer_;
154-
std::unique_ptr<livekit_client_plugin::TaskRunnerLinux> task_runner_;
154+
std::shared_ptr<livekit_client_plugin::TaskRunnerLinux> task_runner_;
155155
std::unique_ptr<flutter::EventChannel<flutter::EncodableValue>> channel_;
156156
std::shared_ptr<flutter::EventSink<flutter::EncodableValue>> sink_;
157157
std::list<flutter::EncodableValue> event_queue_;
@@ -172,7 +172,7 @@ class AudioRendererSink : public libwebrtc::AudioTrackSink {
172172
messenger, event_channel_name,
173173
&flutter::StandardMethodCodec::GetInstance())),
174174
media_track_(media_track), format_(format) {
175-
task_runner_ = std::make_unique<livekit_client_plugin::TaskRunnerLinux>();
175+
task_runner_ = std::make_shared<livekit_client_plugin::TaskRunnerLinux>();
176176
auto handler = std::make_unique<
177177
flutter::StreamHandlerFunctions<flutter::EncodableValue>>(
178178
[&](const flutter::EncodableValue *arguments,
@@ -250,7 +250,7 @@ class AudioRendererSink : public libwebrtc::AudioTrackSink {
250250
}
251251
}
252252

253-
std::unique_ptr<livekit_client_plugin::TaskRunnerLinux> task_runner_;
253+
std::shared_ptr<livekit_client_plugin::TaskRunnerLinux> task_runner_;
254254
std::unique_ptr<flutter::EventChannel<flutter::EncodableValue>> channel_;
255255
std::shared_ptr<flutter::EventSink<flutter::EncodableValue>> sink_;
256256
bool on_listen_called_ = false;

‎linux/task_runner_linux.cc‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,44 @@ void TaskRunnerLinux::EnqueueTask(TaskClosure task) {
1212

1313
GMainContext* context = g_main_context_default();
1414
if (context) {
15-
g_main_context_invoke(
16-
context,
17-
[](gpointer user_data) -> gboolean {
18-
TaskRunnerLinux* runner = static_cast<TaskRunnerLinux*>(user_data);
19-
std::lock_guard<std::mutex> lock(runner->tasks_mutex_);
20-
while (!runner->tasks_.empty()) {
21-
TaskClosure task = std::move(runner->tasks_.front());
22-
runner->tasks_.pop();
23-
task();
15+
// A weak_ptr (not `this`) is passed through so that if the runner is
16+
// destroyed before the main loop dispatches this callback, for example
17+
// when the owning sink is torn down while an audio frame's task is still
18+
// queued, the callback safely no-ops instead of locking a mutex inside
19+
// freed memory. g_main_context_invoke_full's notify always runs exactly
20+
// once, whether the callback fired inline or via the idle source, so the
21+
// heap-allocated weak_ptr is never leaked.
22+
//
23+
// weak_from_this() itself is only safe here because the owning sink is
24+
// destroyed after the audio track's RemoveSink() returns, and RemoveSink()
25+
// blocks until any in-flight OnData() (and therefore this call) has
26+
// finished. Callers must keep that ordering.
27+
auto* weak_self = new std::weak_ptr<TaskRunnerLinux>(weak_from_this());
28+
g_main_context_invoke_full(
29+
context, G_PRIORITY_DEFAULT,
30+
[](gpointer user_data) -> gboolean {
31+
auto* weak_self = static_cast<std::weak_ptr<TaskRunnerLinux>*>(user_data);
32+
if (auto runner = weak_self->lock()) {
33+
// Take the whole batch under the lock, then run it unlocked so a
34+
// task that re-enters EnqueueTask on this runner cannot deadlock
35+
// on the non-recursive mutex.
36+
std::queue<TaskClosure> pending;
37+
{
38+
std::lock_guard<std::mutex> lock(runner->tasks_mutex_);
39+
std::swap(pending, runner->tasks_);
40+
}
41+
while (!pending.empty()) {
42+
TaskClosure task = std::move(pending.front());
43+
pending.pop();
44+
task();
45+
}
2446
}
2547
return G_SOURCE_REMOVE;
2648
},
27-
this);
49+
weak_self,
50+
[](gpointer user_data) {
51+
delete static_cast<std::weak_ptr<TaskRunnerLinux>*>(user_data);
52+
});
2853
}
2954
}
3055

‎linux/task_runner_linux.h‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
#include <memory>
55
#include <mutex>
66
#include <queue>
7-
#include <functional>
8-
9-
using TaskClosure = std::function<void()>;
7+
#include <functional>
8+
9+
using TaskClosure = std::function<void()>;
1010

1111
namespace livekit_client_plugin {
1212

13-
class TaskRunnerLinux {
13+
// Owned via std::shared_ptr by its creator (never std::unique_ptr). EnqueueTask
14+
// schedules a GLib idle callback that runs asynchronously on the main loop, and
15+
// enable_shared_from_this lets that callback hold a weak reference so it can
16+
// detect the runner having been destroyed in the meantime (for example
17+
// stopAudioRenderer tearing down the sink while a callback is still queued)
18+
// instead of dereferencing freed memory.
19+
class TaskRunnerLinux : public std::enable_shared_from_this<TaskRunnerLinux> {
1420
public:
1521
TaskRunnerLinux() = default;
1622
~TaskRunnerLinux() = default;

0 commit comments

Comments
 (0)