From 95f87ab677daff63a03271dbaa7672de054bccba Mon Sep 17 00:00:00 2001 From: Dragos Oancea Date: Tue, 27 Oct 2026 20:29:50 +0200 Subject: [PATCH] MT#55283 jitter_buffer: lock the timer queue when discarding burst packets remove_oldest_packets() took entries out of jb->ttq.entries and freed them without holding jb->ttq.lock. It even dropped jb->lock first, but that lock never protected the tree: the jitter buffer thread pops, runs and re-inserts entries in timerthread_queue_run() under ttq.lock only. So when a burst of packets pushed the buffer over its target capacity, the media thread could modify the GTree at the same time as the jitter buffer thread. Both threads can also pick the same first entry, which is then freed by the discard and sent and freed again by the timer thread. Either way this corrupts the heap and crashes the daemon. Take ttq.lock around the removal, as timerthread_queue_flush() does, and keep jb->lock held. The lock order jb->lock -> ttq.lock is the one timerthread_queue_push() already uses from queue_packet(), and jb_packet_free() takes neither lock. Closes #2225 Change-Id: Ibab99b0258c6a573018f76c1c58ee5518c04dac0 Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 4aba72b51fffb43394a4bd361ac26207d8cae930) --- daemon/jitter_buffer.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/daemon/jitter_buffer.c b/daemon/jitter_buffer.c index dcff122bd..7093e46b5 100644 --- a/daemon/jitter_buffer.c +++ b/daemon/jitter_buffer.c @@ -186,26 +186,29 @@ static struct jb_packet* get_jb_packet(struct media_packet *mp, const str *s) { return p; } -// jb is locked (temporarily unlocked during operation, then relocked) +// jb is locked static int remove_oldest_packets(struct jitter_buffer *jb, int num_to_remove) { if (num_to_remove <= 0) return 0; - + int removed = 0; - mutex_unlock(&jb->lock); - + + // The jitter buffer thread pops and re-inserts entries under the queue's + // own lock, so the tree must only be touched while holding it. Lock order + // jb->lock -> ttq.lock is the same as in timerthread_queue_push(). + LOCK(&jb->ttq.lock); + for (int i = 0; i < num_to_remove; i++) { struct timerthread_queue_entry *ttqe = rtpe_g_tree_first(jb->ttq.entries); if (!ttqe) break; - + g_tree_remove(jb->ttq.entries, ttqe); if (jb->ttq.entry_free_func) jb->ttq.entry_free_func(ttqe); removed++; } - - mutex_lock(&jb->lock); + return removed; }