Skip to content

Commit

Permalink
fix: free msg_env unconditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
savonarola committed Apr 12, 2024
1 parent 72e7014 commit 480a7bc
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 14 deletions.
10 changes: 1 addition & 9 deletions c_src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,16 @@ erlfdb_future_cb(FDBFuture* fdb_future, void* data)
cancelled = future->cancelled;
enif_mutex_unlock(future->cancel_lock);

enif_mutex_lock(future->msg_lock);

if(future->msg_env != NULL) {

if(!cancelled) {
proc_env = (ErlNifEnv*) erl_drv_tsd_get(future->future_proc_env_key);
msg = T2(future->msg_env, future->msg_ref, ATOM_ready);
enif_send(proc_env, &(future->pid), future->msg_env, msg);
} else {
enif_free_env(future->msg_env);
}

enif_free_env(future->msg_env);
future->msg_env = NULL;
}

enif_mutex_unlock(future->msg_lock);

// We're now done with this future which means we need
// to release our handle to it. See erlfdb_create_future
// for more on why this happens here.
Expand Down Expand Up @@ -132,7 +125,6 @@ erlfdb_create_future(ErlNifEnv* env, FDBFuture* future, ErlFDBFutureGetter gette
enif_self(env, &(f->pid));
f->msg_env = enif_alloc_env();
f->msg_ref = enif_make_copy(f->msg_env, ref);
f->msg_lock = enif_mutex_create("fdb:future_msg_lock");

f->cancelled = false;
f->cancel_lock = enif_mutex_create("fdb:future_cancel_lock");
Expand Down
4 changes: 0 additions & 4 deletions c_src/resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ erlfdb_future_dtor(ErlNifEnv* env, void* obj)
if(f->cancel_lock != NULL) {
enif_mutex_destroy(f->cancel_lock);
}

if(f->msg_lock != NULL) {
enif_mutex_destroy(f->msg_lock);
}
}


Expand Down
1 change: 0 additions & 1 deletion c_src/resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ struct _ErlFDBFuture
ErlNifPid pid;
ErlNifEnv* msg_env;
ERL_NIF_TERM msg_ref;
ErlNifMutex* msg_lock;
ErlDrvTSDKey future_proc_env_key;
bool cancelled;
ErlNifMutex* cancel_lock;
Expand Down

0 comments on commit 480a7bc

Please sign in to comment.