From 085fbcc076f2e1be244cd5dc706d5e1cc6338e54 Mon Sep 17 00:00:00 2001 From: William Yang Date: Fri, 8 Sep 2023 16:46:12 +0200 Subject: [PATCH] fix(conn): protect opened c_ctx if reuse handle --- c_src/quicer_connection.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/c_src/quicer_connection.c b/c_src/quicer_connection.c index ec89eb4c..06713355 100644 --- a/c_src/quicer_connection.c +++ b/c_src/quicer_connection.c @@ -608,7 +608,6 @@ async_connect3(ErlNifEnv *env, if (enif_get_map_value(env, eoptions, ATOM_HANDLE, &eHandle)) { // Reuse c_ctx from existing connecion handle - // if (enif_get_resource(env, eHandle, ctx_connection_t, (void **)&c_ctx)) { assert(c_ctx->is_closed); @@ -622,7 +621,6 @@ async_connect3(ErlNifEnv *env, { Registration = GRegistration; } - // @TODO we should take lock here } else { @@ -668,6 +666,13 @@ async_connect3(ErlNifEnv *env, } } + + if (is_reuse_handle) + { + enif_mutex_lock(c_ctx->lock); + } + + assert(c_ctx->owner); // allocate config_resource for client connection if (NULL == (c_ctx->config_resource = init_config_ctx())) @@ -784,6 +789,10 @@ async_connect3(ErlNifEnv *env, enif_monitor_process(NULL, c_ctx, &c_ctx->owner->Pid, &c_ctx->owner_mon); eHandle = enif_make_resource(env, c_ctx); + if(is_reuse_handle) + { + enif_mutex_unlock(c_ctx->lock); + } return SUCCESS(eHandle); Error: @@ -832,6 +841,12 @@ async_connect3(ErlNifEnv *env, } // Error exit, it must be closed or Handle is NULL assert(c_ctx->is_closed || NULL == c_ctx->Connection); + + if(is_reuse_handle) + { + enif_mutex_unlock(c_ctx->lock); + } + return res; }