From 24065b098c698ea8ce2c5adadcda06924ad7c67f Mon Sep 17 00:00:00 2001 From: William Yang Date: Mon, 4 Sep 2023 13:37:49 +0200 Subject: [PATCH] fix(listener): mark listener stopped in resource down callback --- c_src/quicer_nif.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/c_src/quicer_nif.c b/c_src/quicer_nif.c index 2ba329a1..f277564e 100644 --- a/c_src/quicer_nif.c +++ b/c_src/quicer_nif.c @@ -765,14 +765,28 @@ resource_listener_down_callback(__unused_parm__ ErlNifEnv *env, { QuicerListenerCTX *l_ctx = (QuicerListenerCTX *)ctx; TP_CB_3(start, (uintptr_t)l_ctx->Listener, 0); - if (l_ctx->Listener) - { - MsQuic->ListenerStop(l_ctx->Listener); - } - + BOOLEAN to_stop = FALSE; + enif_mutex_lock(l_ctx->lock); + if (!l_ctx->is_closed && !l_ctx->is_stopped && l_ctx->Listener) + { + to_stop = TRUE; + l_ctx->is_stopped = TRUE; + } + enif_mutex_unlock(l_ctx->lock); + if (to_stop) + { + // We only stop here, but not close it, because we want to + // close it in resource_listener_dealloc_callback + // Or some pid could still start the stopped listener with nif handle + MsQuic->ListenerStop(l_ctx->Listener); + } TP_CB_3(end, (uintptr_t)l_ctx->Listener, 0); } + +/* +** Listener NIF handle, end of world... + */ void resource_listener_dealloc_callback(__unused_parm__ ErlNifEnv *env, void *obj) {