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 728c2bab7ad80328e27ea6d66a5141bdd50427cd
Author: Cedric BAIL <[email protected]>
AuthorDate: Sat Nov 7 22:40:40 2026 -0600

    eo: a scheduler survives the deletion of its object by its own batch
    
    _futures_dispatch_cb() runs a batch from a list of its own. When a
    callback of the batch deletes the object that owns the scheduler (a
    parent set to NULL, an unref), _futures_cancel_cb() freed or trashed the
    scheduler under the loop: the rest of the batch then ran with an entry
    whose scheduler was gone, and a later recall read sched->futures from
    freed memory. The entries of the batch were not reachable from the
    scheduler, so they were neither canceled nor skipped.
    
    A callback of a batch can also start another batch of the same
    scheduler, by spinning a nested main loop, so the batches being run are
    kept as a chain of frames linked from the scheduler, the innermost
    first. Deleted meanwhile, the scheduler cancels the entries not yet run
    in every frame (the recall marks them and the loops skip them), is
    marked dead, and is freed by the outermost loop when it is over. An
    entry is counted as called before its callback runs so the deletion
    leaves the running one alone.
    
    The new tests delete the owner from the first callback of a batch of
    three, and from a batch nested in the first callback of another: every
    entry left is told once that it was canceled and never run for its
    value, nothing is logged as an error, and valgrind reports no invalid
    read (it reported several before the change).
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/lib/eo/eo_base_class.c             | 79 ++++++++++++++++++++++++++----
 src/tests/ecore/efl_app_test_promise.c | 87 ++++++++++++++++++++++++++++++++++
 2 files changed, 156 insertions(+), 10 deletions(-)

diff --git a/src/lib/eo/eo_base_class.c b/src/lib/eo/eo_base_class.c
index b2779d341e..390dfb930f 100644
--- a/src/lib/eo/eo_base_class.c
+++ b/src/lib/eo/eo_base_class.c
@@ -1798,6 +1798,14 @@ EO_API EFL_FUNC_BODYV(efl_event_callback_array_del,
 typedef struct _Efl_Future_Scheduler Efl_Future_Scheduler;
 typedef struct _Efl_Future_Scheduler_Entry Efl_Future_Scheduler_Entry;
 
+typedef struct _Efl_Future_Batch Efl_Future_Batch;
+
+struct _Efl_Future_Batch
+{
+   Eina_List *entries;
+   Efl_Future_Batch *prev;
+};
+
 struct _Efl_Future_Scheduler
 {
    Eina_Future_Scheduler scheduler;
@@ -1806,8 +1814,15 @@ struct _Efl_Future_Scheduler
    const Eo *self;
 
    Eina_List *futures;
+   // The batches being run by _futures_dispatch_cb(), the innermost first (a
+   // callback may start another one of the same scheduler): the entries that
+   // are not called yet can be reached when the scheduler is deleted.
+   Efl_Future_Batch *batches;
+   unsigned int dispatching;
 
    Eina_Bool listener : 1;
+   // Deleted while a batch was running: freed when the last batch is over.
+   Eina_Bool dead : 1;
 };
 
 struct _Efl_Future_Scheduler_Entry
@@ -1834,6 +1849,20 @@ _future_scheduler_cleanup(Efl_Object_Data *pd)
    _efl_object_extension_noneed(pd);
 }
 
+static void
+_future_scheduler_release(Efl_Future_Scheduler *sched)
+{
+   if (schedulers_count > 8)
+     {
+        free(sched);
+     }
+   else
+     {
+        eina_trash_push(&schedulers_trash, sched);
+        schedulers_count++;
+     }
+}
+
 static void
 _futures_dispatch_cb(void *data, const Efl_Event *ev EINA_UNUSED)
 {
@@ -1841,20 +1870,31 @@ _futures_dispatch_cb(void *data, const Efl_Event *ev EINA_UNUSED)
    Eina_List *entries = sched->futures;
    Efl_Future_Scheduler_Entry *entry;
    Eina_List *l;
+   Efl_Future_Batch frame;
 
    sched->futures = NULL;
 
    efl_event_callback_array_del((Eo *) sched->self, sched->array, sched);
    sched->listener = EINA_FALSE;
 
+   // The scheduler may be deleted by a callback of the batch (its object goes
+   // away): it is then kept until the batch is over.
+   frame.entries = entries;
+   frame.prev = sched->batches;
+   sched->batches = &frame;
+   sched->dispatching++;
+
    // Now trigger callbacks. A callback may cancel a future of this batch, which
    // frees the future and recalls its entry: that entry must not be called.
+   // An entry is counted as called before its callback runs, so that the
+   // deletion of the scheduler, from this callback, leaves it alone.
    EINA_LIST_FOREACH(entries, l, entry)
      {
-        if ((entry->called == 0) && !entry->recalled)
+        if ((entry->called++ == 0) && !entry->recalled)
           entry->cb(entry->future, entry->value);
-        entry->called++;
      }
+   sched->dispatching--;
+   sched->batches = frame.prev;
    EINA_LIST_FREE(entries, entry)
      {
         if (entry->called > 1) ERR("Future entry called more than once!");
@@ -1862,6 +1902,8 @@ _futures_dispatch_cb(void *data, const Efl_Event *ev EINA_UNUSED)
         if (entry->called == 0)
            eina_mempool_free(_efl_future_scheduler_entry_mempool, entry);
      }
+   // Deleted meanwhile: its object was told, and the rest of the batch canceled.
+   if (!sched->dispatching && sched->dead) _future_scheduler_release(sched);
 }
 
 static void
@@ -1883,15 +1925,26 @@ _futures_cancel_cb(void *data)
         eina_mempool_free(_efl_future_scheduler_entry_mempool, entry);
      }
 
-   if (schedulers_count > 8)
+   if (sched->dispatching)
      {
-        free(sched);
-     }
-   else
-     {
-        eina_trash_push(&schedulers_trash, sched);
-        schedulers_count++;
+        // A callback of the batch being run deleted the object. The entries
+        // of that batch that did not run are canceled (the loop skips them
+        // and frees them), and the scheduler is freed by the loop when done.
+        Efl_Future_Batch *b;
+        Eina_List *l;
+
+        sched->dead = EINA_TRUE;
+        for (b = sched->batches; b; b = b->prev)
+          {
+             EINA_LIST_FOREACH(b->entries, l, entry)
+               {
+                  if ((entry->called == 0) && !entry->recalled)
+                    eina_future_cancel(entry->future);
+               }
+          }
+        return;
      }
+   _future_scheduler_release(sched);
 }
 
 static Eina_Future_Schedule_Entry *
@@ -1947,7 +2000,8 @@ _efl_event_future_recall(Eina_Future_Schedule_Entry *s_entry)
      }
 
    sched->futures = eina_list_remove_list(sched->futures, lookup);
-   if (!sched->futures)
+   // (a scheduler that runs a batch is not cleaned up from under it)
+   if (!sched->futures && !sched->dispatching)
      {
         Efl_Object_Data *pd = efl_data_scope_get(sched->self, EFL_OBJECT_CLASS);
 
@@ -1991,6 +2045,11 @@ _efl_object_event_future_scheduler_get(const Eo *obj, Efl_Object_Data *pd, Efl_C
         // Need to allocate a new scheduler as none are on standby.
         sched = calloc(1, sizeof (Efl_Future_Scheduler));
      }
+   sched->batches = NULL;
+   sched->dispatching = 0;
+   sched->dead = EINA_FALSE;
+   sched->futures = NULL;
+   sched->listener = EINA_FALSE;
    sched->scheduler.schedule = _efl_event_future_scheduler;
    sched->scheduler.recall = _efl_event_future_recall;
    sched->array = array;
diff --git a/src/tests/ecore/efl_app_test_promise.c b/src/tests/ecore/efl_app_test_promise.c
index 75a8af7bc4..cd0fe06e4e 100644
--- a/src/tests/ecore/efl_app_test_promise.c
+++ b/src/tests/ecore/efl_app_test_promise.c
@@ -740,11 +740,25 @@ EFL_SCHEDULER_ARRAY_DEFINE(_owned_sched, &_owned_sched_event);
 typedef struct _Owned_Ctx
 {
    Eo *owner;
+   Eina_Bool kill_owner;
+   int first_ran;
+   int nested_ran;
    int ran;
    int canceled;
    int errors;
 } Owned_Ctx;
 
+static Eina_Value
+_owned_first(void *data, const Eina_Value v EINA_UNUSED, const Eina_Future *dead EINA_UNUSED)
+{
+   Owned_Ctx *ctx = data;
+
+   ctx->first_ran++;
+   // The object that owns the scheduler goes away from a callback of the batch.
+   if (ctx->kill_owner) efl_parent_set(ctx->owner, NULL);
+   return EINA_VALUE_EMPTY;
+}
+
 static Eina_Value
 _owned_other(void *data, const Eina_Value v, const Eina_Future *dead EINA_UNUSED)
 {
@@ -812,6 +826,77 @@ EFL_START_TEST(efl_test_promise_future_owner_destroyed)
 }
 EFL_END_TEST
 
+// The owner is destroyed by a callback of the batch being run: what is left of
+// the batch is canceled, not run, and nothing reads the scheduler once freed.
+EFL_START_TEST(efl_test_promise_future_owner_destroyed_in_batch)
+{
+   Owned_Ctx ctx = { 0 };
+   Eo *parent;
+
+   fail_if(!ecore_init());
+   parent = efl_add_ref(EFL_IO_BUFFER_CLASS, NULL);
+   ctx.owner = efl_add(EFL_IO_BUFFER_CLASS, parent);
+   fail_if(!ctx.owner);
+   ctx.kill_owner = EINA_TRUE;
+   _owned_future(ctx.owner, _owned_first, &ctx, "first");
+   _owned_future(ctx.owner, _owned_other, &ctx, "second");
+   _owned_future(ctx.owner, _owned_other, &ctx, "third");
+   eina_log_print_cb_set(_owned_log, &ctx);
+   efl_event_callback_call(ctx.owner, &_owned_sched_event, NULL);
+   eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
+   ck_assert_int_eq(ctx.first_ran, 1);
+   // each of the others is told it was canceled, once, and never ran for its value
+   ck_assert_int_eq(ctx.ran, 2);
+   ck_assert_int_eq(ctx.canceled, 2);
+   ck_assert_int_eq(ctx.errors, 0);
+   efl_unref(parent);
+   ecore_shutdown();
+}
+EFL_END_TEST
+
+static Eina_Value
+_nested_a1(void *data, const Eina_Value v EINA_UNUSED, const Eina_Future *dead EINA_UNUSED)
+{
+   Owned_Ctx *ctx = data;
+
+   ctx->nested_ran++;
+   // A second batch of the same scheduler starts inside this callback, as it
+   // does when a callback spins a nested main loop...
+   _owned_future(ctx->owner, _owned_first, ctx, "b1");
+   _owned_future(ctx->owner, _owned_other, ctx, "b2");
+   efl_event_callback_call(ctx->owner, &_owned_sched_event, NULL);
+   return EINA_VALUE_EMPTY;
+}
+
+// ...and the second one deletes the owner (_owned_first): what is left of both
+// batches is canceled, not run, and the scheduler is freed once, by the outer loop.
+EFL_START_TEST(efl_test_promise_future_owner_destroyed_in_nested_batch)
+{
+   Owned_Ctx ctx = { 0 };
+   Eo *parent;
+
+   fail_if(!ecore_init());
+   parent = efl_add_ref(EFL_IO_BUFFER_CLASS, NULL);
+   ctx.owner = efl_add(EFL_IO_BUFFER_CLASS, parent);
+   fail_if(!ctx.owner);
+   ctx.kill_owner = EINA_TRUE;
+   _owned_future(ctx.owner, _nested_a1, &ctx, "a1");
+   _owned_future(ctx.owner, _owned_other, &ctx, "a2");
+   _owned_future(ctx.owner, _owned_other, &ctx, "a3");
+   eina_log_print_cb_set(_owned_log, &ctx);
+   efl_event_callback_call(ctx.owner, &_owned_sched_event, NULL);
+   eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
+   ck_assert_int_eq(ctx.nested_ran, 1);
+   ck_assert_int_eq(ctx.first_ran, 1);
+   // a2, a3 (outer batch) and b2 (inner one): told once, never run for their value
+   ck_assert_int_eq(ctx.ran, 3);
+   ck_assert_int_eq(ctx.canceled, 3);
+   ck_assert_int_eq(ctx.errors, 0);
+   efl_unref(parent);
+   ecore_shutdown();
+}
+EFL_END_TEST
+
 EFL_START_TEST(efl_test_promise_future_inner_promise)
 {
    Eina_Future *f;
@@ -1658,6 +1743,8 @@ void efl_app_test_promise_2(TCase *tc)
    tcase_add_test(tc, efl_test_promise_future_cancel);
    tcase_add_test(tc, efl_test_promise_future_cancel_in_batch);
    tcase_add_test(tc, efl_test_promise_future_owner_destroyed);
+   tcase_add_test(tc, efl_test_promise_future_owner_destroyed_in_batch);
+   tcase_add_test(tc, efl_test_promise_future_owner_destroyed_in_nested_batch);
    tcase_add_test(tc, efl_test_promise_future_implicit_cancel);
    tcase_add_test(tc, efl_test_promise_future_inner_promise);
    tcase_add_test(tc, efl_test_promise_future_inner_promise_fail);

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

Reply via email to