From 8a3eb523d2747b8637eda989799821db0e78d360 Mon Sep 17 00:00:00 2001 From: Ilya Averyanov Date: Sat, 6 Apr 2024 17:48:25 +0300 Subject: [PATCH 1/2] fix: do not try to save future creator's env --- c_src/main.c | 14 +------------- c_src/resources.h | 1 - 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/c_src/main.c b/c_src/main.c index 9a96400..8778cca 100644 --- a/c_src/main.c +++ b/c_src/main.c @@ -75,24 +75,13 @@ static void erlfdb_future_cb(FDBFuture* fdb_future, void* data) { ErlFDBFuture* future = (ErlFDBFuture*) data; - ErlNifEnv* caller; ERL_NIF_TERM msg; - // FoundationDB callbacks can fire from the thread - // that created them. Check if we were actually - // submitted to the network thread or not so that - // we pass the correct environment to enif_send - if(enif_thread_type() == ERL_NIF_THR_UNDEFINED) { - caller = NULL; - } else { - caller = future->pid_env; - } - enif_mutex_lock(future->lock); if(!future->cancelled) { msg = T2(future->msg_env, future->msg_ref, ATOM_ready); - enif_send(caller, &(future->pid), future->msg_env, msg); + enif_send(NULL, &(future->pid), future->msg_env, msg); } enif_mutex_unlock(future->lock); @@ -119,7 +108,6 @@ erlfdb_create_future(ErlNifEnv* env, FDBFuture* future, ErlFDBFutureGetter gette f->future = future; f->fgetter = getter; enif_self(env, &(f->pid)); - f->pid_env = env; f->msg_env = enif_alloc_env(); f->msg_ref = enif_make_copy(f->msg_env, ref); f->lock = enif_mutex_create("fdb:future_lock"); diff --git a/c_src/resources.h b/c_src/resources.h index 42e0630..265f725 100644 --- a/c_src/resources.h +++ b/c_src/resources.h @@ -33,7 +33,6 @@ struct _ErlFDBFuture FDBFuture* future; ErlFDBFutureGetter fgetter; ErlNifPid pid; - ErlNifEnv* pid_env; ErlNifEnv* msg_env; ERL_NIF_TERM msg_ref; ErlNifMutex* lock; From 5636dc895a1431ca725831f4b4b5dcd8b1d2c38d Mon Sep 17 00:00:00 2001 From: Ilya Averyanov Date: Sun, 7 Apr 2024 23:57:02 +0300 Subject: [PATCH 2/2] fix: span future lock only on field operations --- c_src/main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/c_src/main.c b/c_src/main.c index 8778cca..090ff70 100644 --- a/c_src/main.c +++ b/c_src/main.c @@ -76,15 +76,18 @@ erlfdb_future_cb(FDBFuture* fdb_future, void* data) { ErlFDBFuture* future = (ErlFDBFuture*) data; ERL_NIF_TERM msg; + bool cancelled; + enif_mutex_lock(future->lock); + cancelled = future->cancelled; + enif_mutex_unlock(future->lock); - if(!future->cancelled) { + if(!cancelled) { msg = T2(future->msg_env, future->msg_ref, ATOM_ready); enif_send(NULL, &(future->pid), future->msg_env, msg); } - enif_mutex_unlock(future->lock); // We're now done with this future which means we need // to release our handle to it. See erlfdb_create_future @@ -557,9 +560,10 @@ erlfdb_future_cancel(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) enif_mutex_lock(future->lock); future->cancelled = true; + enif_mutex_unlock(future->lock); + fdb_future_cancel(future->future); - enif_mutex_unlock(future->lock); return ATOM_ok; }