This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository efl.

View the commit online.

commit c982f7b670987f2f450c93833c6100eebb9755bf
Author: Cedric BAIL <[email protected]>
AuthorDate: Wed Nov 11 21:02:49 2026 -0600

    eina: freeing a thread queue frees the messages nobody holds
    
    eina_thread_queue_free released the queue itself but not the message
    blocks holding what had been sent and never fetched, so a queue freed
    with a reply nobody collected (the ecore_con proxy helper does this)
    leaked a 4096 byte block each time. Those blocks are now given back, to
    the pool or the heap, as when they are fetched.
    
    A block someone still uses is left alone, as before:
    
    - a listener may hold a message fetched from it, and release it with
      eina_thread_queue_wait_done() after the queue is gone: that call
      never needed the queue. Freeing the block would turn that into a use
      after free, and put the block in the pool twice.
    - a sender may be in the middle of a message, holding the block's
      lock_non_0_ref. After a fork that sender is gone and the lock stays
      taken (evas_scale_sample frees its queues from a fork reset
      callback): taking it to free the block would never return.
    
    So nothing that worked before changes behaviour; only blocks that
    nothing references are freed.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/lib/eina/eina_thread_queue.c                   | 33 ++++++++++
 src/lib/eina/eina_thread_queue.h                   |  4 ++
 .../ecore_test_ecore_thread_eina_thread_queue.c    | 70 ++++++++++++++++++++++
 3 files changed, 107 insertions(+)

diff --git a/src/lib/eina/eina_thread_queue.c b/src/lib/eina/eina_thread_queue.c
index 1343b8d393..b70b424d77 100644
--- a/src/lib/eina/eina_thread_queue.c
+++ b/src/lib/eina/eina_thread_queue.c
@@ -188,6 +188,30 @@ _eina_thread_queue_msg_block_free(Eina_Thread_Queue_Msg_Block *blk)
    else _eina_thread_queue_msg_block_real_free(blk);
 }
 
+/* Frees a block of a queue being freed, unless someone still uses it: a
+ * listener holding a message fetched from it (its ref), which may still
+ * call eina_thread_queue_wait_done() since that does not need the queue,
+ * or a sender in the middle of a message (its lock_non_0_ref, which may
+ * also be held by a thread that is gone after a fork). Such a block is
+ * left alone, as it was before the queue freed any block. */
+static void
+_eina_thread_queue_msg_block_free_unused(Eina_Thread_Queue_Msg_Block *blk)
+{
+   int ref;
+
+#ifdef ATOMIC
+   __atomic_load(&(blk->ref), &ref, __ATOMIC_RELAXED);
+#else
+   eina_spinlock_take(&(blk->lock_ref));
+   ref = blk->ref;
+   eina_spinlock_release(&(blk->lock_ref));
+#endif
+   if (ref != 0) return;
+   if (eina_lock_take_try(&(blk->lock_non_0_ref)) != EINA_LOCK_SUCCEED) return;
+   eina_lock_release(&(blk->lock_non_0_ref));
+   _eina_thread_queue_msg_block_free(blk);
+}
+
 static Eina_Bool
 _eina_thread_queue_msg_block_pool_init(void)
 {
@@ -456,8 +480,17 @@ eina_thread_queue_new(void)
 EINA_API void
 eina_thread_queue_free(Eina_Thread_Queue *thq)
 {
+   Eina_Thread_Queue_Msg_Block *blk, *next;
+
    if (!thq) return;
 
+   if (thq->read) _eina_thread_queue_msg_block_free_unused(thq->read);
+   for (blk = thq->data; blk; blk = next)
+     {
+        next = blk->next;
+        _eina_thread_queue_msg_block_free_unused(blk);
+     }
+
 #ifndef ATOMIC
    eina_spinlock_free(&(thq->lock_pending));
 #endif
diff --git a/src/lib/eina/eina_thread_queue.h b/src/lib/eina/eina_thread_queue.h
index 094d8bd41b..5ca7ae6508 100644
--- a/src/lib/eina/eina_thread_queue.h
+++ b/src/lib/eina/eina_thread_queue.h
@@ -83,6 +83,10 @@ eina_thread_queue_new(void);
  * on messages or sending them. Any pending messages will be freed without
  * being processed by a listener.
  *
+ * The messages that were sent and never fetched are freed with the queue. A
+ * message fetched and not yet released with eina_thread_queue_wait_done()
+ * stays valid, and may still be released after the queue is freed.
+ *
  * @param[in] thq The thread queue to free
  *
  * @since 1.11
diff --git a/src/tests/ecore/ecore_test_ecore_thread_eina_thread_queue.c b/src/tests/ecore/ecore_test_ecore_thread_eina_thread_queue.c
index a2dfe55993..cabfc29d59 100644
--- a/src/tests/ecore/ecore_test_ecore_thread_eina_thread_queue.c
+++ b/src/tests/ecore/ecore_test_ecore_thread_eina_thread_queue.c
@@ -680,11 +680,81 @@ EFL_START_TEST(ecore_test_ecore_thread_eina_thread_queue_t7)
 }
 EFL_END_TEST
 
+/* A queue freed with messages in it, across several blocks and half read.
+ * Nothing to observe but a clean run; valgrind is what shows the blocks
+ * going back. */
+EFL_START_TEST(ecore_test_ecore_thread_eina_thread_queue_free_unread)
+{
+   Eina_Thread_Queue *thq = eina_thread_queue_new();
+   Msg *msg;
+   void *ref;
+   int i;
+
+   fail_if(!thq);
+   for (i = 0; i < 500; i++)
+     {
+        msg = eina_thread_queue_send(thq, sizeof(Msg), &ref);
+        fail_if(!msg);
+        msg->value = i;
+        eina_thread_queue_send_done(thq, ref);
+     }
+   /* one message larger than a block */
+   msg = eina_thread_queue_send(thq, 8192, &ref);
+   fail_if(!msg);
+   eina_thread_queue_send_done(thq, ref);
+
+   msg = eina_thread_queue_wait(thq, &ref);
+   fail_if(!msg);
+   fail_if(msg->value != 0);
+   eina_thread_queue_wait_done(thq, ref);
+
+   fail_if(eina_thread_queue_pending_get(thq) != 500);
+   eina_thread_queue_free(thq);
+}
+EFL_END_TEST
+
+/* A message fetched and not released yet stays valid when its queue is
+ * freed, and is released after: wait_done does not need the queue. Its block
+ * must not go back to the pool, where the next queue would write over it. */
+EFL_START_TEST(ecore_test_ecore_thread_eina_thread_queue_free_while_held)
+{
+   Eina_Thread_Queue *thq = eina_thread_queue_new(), *other;
+   Msg *msg, *held;
+   void *ref, *held_ref;
+   int i;
+
+   fail_if(!thq);
+   for (i = 0; i < 3; i++)
+     {
+        msg = eina_thread_queue_send(thq, sizeof(Msg), &ref);
+        fail_if(!msg);
+        msg->value = 42 + i;
+        eina_thread_queue_send_done(thq, ref);
+     }
+   held = eina_thread_queue_wait(thq, &held_ref);
+   fail_if(!held);
+   eina_thread_queue_free(thq);
+
+   other = eina_thread_queue_new();
+   fail_if(!other);
+   msg = eina_thread_queue_send(other, sizeof(Msg), &ref);
+   fail_if(!msg);
+   msg->value = 7;
+   eina_thread_queue_send_done(other, ref);
+
+   ck_assert_int_eq(held->value, 42);
+   eina_thread_queue_wait_done(NULL, held_ref);
+   eina_thread_queue_free(other);
+}
+EFL_END_TEST
+
 void ecore_test_ecore_thread_eina_thread_queue(TCase *tc EINA_UNUSED)
 {
    tcase_add_test(tc, ecore_test_ecore_thread_eina_thread_queue_t1);
    tcase_add_test(tc, ecore_test_ecore_thread_eina_thread_queue_t2);
    tcase_add_test(tc, ecore_test_ecore_thread_eina_thread_queue_t3);
+   tcase_add_test(tc, ecore_test_ecore_thread_eina_thread_queue_free_unread);
+   tcase_add_test(tc, ecore_test_ecore_thread_eina_thread_queue_free_while_held);
 }
 
 void ecore_test_ecore_thread_eina_thread_queue2(TCase *tc EINA_UNUSED)

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to