Skip to content

Commit

Permalink
fix(conn): protect opened c_ctx if reuse handle
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhuyan committed Sep 11, 2023
1 parent 21d390b commit 085fbcc
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions c_src/quicer_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -622,7 +621,6 @@ async_connect3(ErlNifEnv *env,
{
Registration = GRegistration;
}
// @TODO we should take lock here
}
else
{
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 085fbcc

Please sign in to comment.