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 b19c446bef682c3cbca4004fab59ace93787e09e
Author: Cedric BAIL <[email protected]>
AuthorDate: Sat Nov 7 22:10:51 2026 -0600
eo: a future canceled from its own batch is not called again
The scheduler of an object runs the futures it holds in batches: it
moves them to a list of its own, empties sched->futures, and calls each
entry. When a callback of the batch cancels a later future of the same
batch, eina_future_cancel() recalls the entry of that future and frees
the future. The recall looked the entry up in sched->futures, which no
longer holds the batch, found nothing and returned, so the entry kept
its value and was called with the freed future afterwards: the
"f->scheduled_entry == NULL" safety error, and a read of freed (and
possibly reused) memory.
An entry the recall cannot find is in the batch being run: mark it
recalled, flush its value, and let the batch skip it and free it as it
does the others. The mark is cleared when an entry is scheduled, as the
entries come from a mempool that does not clear them, and the scheduler
cancelling its entries on deletion no longer flushes a value the recall
already flushed.
The new tests cancel the second of two futures of the loop scheduler
from the callback of the first, and delete the owner of a scheduler
holding a stringshare value; under valgrind both reported invalid reads
or a double free before the change and nothing after.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
src/lib/eo/eo_base_class.c | 25 ++++-
src/tests/ecore/efl_app_test_promise.c | 171 +++++++++++++++++++++++++++++++++
2 files changed, 192 insertions(+), 4 deletions(-)
diff --git a/src/lib/eo/eo_base_class.c b/src/lib/eo/eo_base_class.c
index 8f31156106..b2779d341e 100644
--- a/src/lib/eo/eo_base_class.c
+++ b/src/lib/eo/eo_base_class.c
@@ -1817,6 +1817,8 @@ struct _Efl_Future_Scheduler_Entry
Eina_Future *future;
Eina_Value value;
int called;
+ // Recalled while the batch it is in was being run: the future is gone.
+ Eina_Bool recalled : 1;
};
static Eina_Trash *schedulers_trash = NULL;
@@ -1845,10 +1847,12 @@ _futures_dispatch_cb(void *data, const Efl_Event *ev EINA_UNUSED)
efl_event_callback_array_del((Eo *) sched->self, sched->array, sched);
sched->listener = EINA_FALSE;
- // Now trigger callbacks
+ // 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.
EINA_LIST_FOREACH(entries, l, entry)
{
- if (entry->called == 0) entry->cb(entry->future, entry->value);
+ if ((entry->called == 0) && !entry->recalled)
+ entry->cb(entry->future, entry->value);
entry->called++;
}
EINA_LIST_FREE(entries, entry)
@@ -1874,7 +1878,8 @@ _futures_cancel_cb(void *data)
EINA_LIST_FREE(entries, entry)
{
eina_future_cancel(entry->future);
- eina_value_flush(&entry->value);
+ // The cancel recalled the entry, which flushed its value.
+ if (!entry->recalled) eina_value_flush(&entry->value);
eina_mempool_free(_efl_future_scheduler_entry_mempool, entry);
}
@@ -1906,6 +1911,7 @@ _efl_event_future_scheduler(Eina_Future_Scheduler *s_sched,
entry->future = future;
entry->value = value;
entry->called = 0;
+ entry->recalled = EINA_FALSE;
if (!sched->listener)
{
@@ -1927,7 +1933,18 @@ _efl_event_future_recall(Eina_Future_Schedule_Entry *s_entry)
sched = (Efl_Future_Scheduler *) entry->base.scheduler;
lookup = eina_list_data_find_list(sched->futures, entry);
- if (!lookup) return;
+ if (!lookup)
+ {
+ // Not waiting for a batch: it is in the batch being run, whose list is
+ // local to _futures_dispatch_cb(), and the future it names is about to
+ // be freed. The batch frees the entry, but must skip it.
+ if (!entry->recalled)
+ {
+ entry->recalled = EINA_TRUE;
+ eina_value_flush(&entry->value);
+ }
+ return;
+ }
sched->futures = eina_list_remove_list(sched->futures, lookup);
if (!sched->futures)
diff --git a/src/tests/ecore/efl_app_test_promise.c b/src/tests/ecore/efl_app_test_promise.c
index 2390e29e1f..75a8af7bc4 100644
--- a/src/tests/ecore/efl_app_test_promise.c
+++ b/src/tests/ecore/efl_app_test_promise.c
@@ -643,6 +643,175 @@ EFL_START_TEST(efl_test_promise_future_cancel)
}
EFL_END_TEST
+typedef struct _Batch_Ctx
+{
+ Eina_Future *victim;
+ int first_ran;
+ int victim_ran;
+ int victim_canceled;
+ int errors;
+} Batch_Ctx;
+
+static Eina_Value
+_batch_first(void *data, const Eina_Value v EINA_UNUSED, const Eina_Future *dead EINA_UNUSED)
+{
+ Batch_Ctx *ctx = data;
+
+ ctx->first_ran++;
+ // Both futures are in the batch the scheduler is running: the second is
+ // canceled from the callback of the first.
+ eina_future_cancel(ctx->victim);
+ return EINA_VALUE_EMPTY;
+}
+
+static Eina_Value
+_batch_victim(void *data, const Eina_Value v, const Eina_Future *dead EINA_UNUSED)
+{
+ Batch_Ctx *ctx = data;
+ Eina_Error err = 0;
+
+ ctx->victim_ran++;
+ if (eina_value_type_get(&v) == EINA_VALUE_TYPE_ERROR)
+ {
+ eina_value_get(&v, &err);
+ if (err == ECANCELED) ctx->victim_canceled++;
+ }
+ return EINA_VALUE_EMPTY;
+}
+
+static void
+_batch_log(const Eina_Log_Domain *d EINA_UNUSED, Eina_Log_Level level,
+ const char *file EINA_UNUSED, const char *fnc EINA_UNUSED, int line EINA_UNUSED,
+ const char *fmt EINA_UNUSED, void *data, va_list args EINA_UNUSED)
+{
+ Batch_Ctx *ctx = data;
+
+ if (level <= EINA_LOG_LEVEL_ERR) ctx->errors++;
+}
+
+static void
+_batch_promise_cancel(void *data EINA_UNUSED, const Eina_Promise *dead EINA_UNUSED)
+{
+}
+
+// A future canceled from a callback of the batch the scheduler of the loop is
+// running must not be called again, nor read again, once it is freed.
+EFL_START_TEST(efl_test_promise_future_cancel_in_batch)
+{
+ Batch_Ctx ctx = { 0 };
+ Eina_Promise *p1, *p2;
+ Eina_Future *f1, *f2;
+ int i;
+
+ fail_if(!ecore_init());
+ p1 = eina_promise_new(_future_scheduler_get(), _batch_promise_cancel, NULL);
+ p2 = eina_promise_new(_future_scheduler_get(), _batch_promise_cancel, NULL);
+ fail_if(!p1 || !p2);
+ f1 = eina_future_new(p1);
+ f2 = eina_future_new(p2);
+ fail_if(!f1 || !f2);
+ fail_if(!eina_future_then(f1, _batch_first, &ctx));
+ ctx.victim = eina_future_then(f2, _batch_victim, &ctx);
+ fail_if(!ctx.victim);
+
+ eina_log_print_cb_set(_batch_log, &ctx);
+ eina_promise_resolve(p1, eina_value_int_init(1));
+ eina_promise_resolve(p2, eina_value_int_init(2));
+ for (i = 0; i < 3; i++)
+ {
+ ecore_main_loop_iterate();
+ }
+ eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
+
+ ck_assert_int_eq(ctx.first_ran, 1);
+ // told once that it was canceled, never run for the value it was resolved with
+ ck_assert_int_eq(ctx.victim_ran, 1);
+ ck_assert_int_eq(ctx.victim_canceled, 1);
+ // and nobody read the freed future (the safety check of the scheduler entry)
+ ck_assert_int_eq(ctx.errors, 0);
+ ecore_shutdown();
+}
+EFL_END_TEST
+
+// A scheduler of an object of its own, run by an event the test calls.
+static const Efl_Event_Description _owned_sched_event = EFL_EVENT_DESCRIPTION("test,owned,sched");
+EFL_SCHEDULER_ARRAY_DEFINE(_owned_sched, &_owned_sched_event);
+
+typedef struct _Owned_Ctx
+{
+ Eo *owner;
+ int ran;
+ int canceled;
+ int errors;
+} Owned_Ctx;
+
+static Eina_Value
+_owned_other(void *data, const Eina_Value v, const Eina_Future *dead EINA_UNUSED)
+{
+ Owned_Ctx *ctx = data;
+ Eina_Error err = 0;
+
+ ctx->ran++;
+ if (eina_value_type_get(&v) == EINA_VALUE_TYPE_ERROR)
+ {
+ eina_value_get(&v, &err);
+ if (err == ECANCELED) ctx->canceled++;
+ }
+ return EINA_VALUE_EMPTY;
+}
+
+static void
+_owned_log(const Eina_Log_Domain *d EINA_UNUSED, Eina_Log_Level level,
+ const char *file EINA_UNUSED, const char *fnc EINA_UNUSED, int line EINA_UNUSED,
+ const char *fmt EINA_UNUSED, void *data, va_list args EINA_UNUSED)
+{
+ Owned_Ctx *ctx = data;
+
+ if (level <= EINA_LOG_LEVEL_ERR) ctx->errors++;
+}
+
+static Eina_Future *
+_owned_future(Eo *owner, Eina_Future_Cb cb, Owned_Ctx *ctx, const char *str)
+{
+ Eina_Promise *p = eina_promise_new(efl_event_future_scheduler_get(owner, _owned_sched()),
+ _batch_promise_cancel, NULL);
+ Eina_Future *f;
+ Eina_Value v;
+
+ fail_if(!p);
+ f = eina_future_new(p);
+ fail_if(!f);
+ f = eina_future_then(f, cb, ctx);
+ fail_if(!f);
+ fail_if(!eina_value_setup(&v, EINA_VALUE_TYPE_STRINGSHARE));
+ fail_if(!eina_value_set(&v, str));
+ eina_promise_resolve(p, v);
+ return f;
+}
+
+// The owner of a scheduler is destroyed while a future with a value that
+// holds memory (a stringshare) is scheduled in it: the value is freed once.
+EFL_START_TEST(efl_test_promise_future_owner_destroyed)
+{
+ 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);
+ _owned_future(ctx.owner, _owned_other, &ctx, "scheduled and never run");
+ eina_log_print_cb_set(_owned_log, &ctx);
+ efl_parent_set(ctx.owner, NULL);
+ eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
+ ck_assert_int_eq(ctx.ran, 1);
+ ck_assert_int_eq(ctx.canceled, 1);
+ 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;
@@ -1487,6 +1656,8 @@ void efl_app_test_promise_2(TCase *tc)
tcase_add_test(tc, efl_test_promise_future_chain_no_error);
tcase_add_test(tc, efl_test_promise_future_chain_error);
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_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.