From 3b10d80e64023148a478aa34f0032d5f27292a32 Mon Sep 17 00:00:00 2001 From: William Yang Date: Thu, 11 Apr 2024 10:27:33 +0200 Subject: [PATCH] tmp: avoid dead lock in callback --- c_src/main.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/c_src/main.c b/c_src/main.c index cfd40e1..a9359e5 100644 --- a/c_src/main.c +++ b/c_src/main.c @@ -79,19 +79,22 @@ erlfdb_future_cb(FDBFuture* fdb_future, void* data) // 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) { + enif_mutex_lock(future->lock); 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_mutex_unlock(future->lock); + if (!caller) { + 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