Skip to content

Commit 24065b0

Browse files
committed
fix(listener): mark listener stopped in resource down callback
1 parent a745039 commit 24065b0

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

c_src/quicer_nif.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -765,14 +765,28 @@ resource_listener_down_callback(__unused_parm__ ErlNifEnv *env,
765765
{
766766
QuicerListenerCTX *l_ctx = (QuicerListenerCTX *)ctx;
767767
TP_CB_3(start, (uintptr_t)l_ctx->Listener, 0);
768-
if (l_ctx->Listener)
769-
{
770-
MsQuic->ListenerStop(l_ctx->Listener);
771-
}
772-
768+
BOOLEAN to_stop = FALSE;
769+
enif_mutex_lock(l_ctx->lock);
770+
if (!l_ctx->is_closed && !l_ctx->is_stopped && l_ctx->Listener)
771+
{
772+
to_stop = TRUE;
773+
l_ctx->is_stopped = TRUE;
774+
}
775+
enif_mutex_unlock(l_ctx->lock);
776+
if (to_stop)
777+
{
778+
// We only stop here, but not close it, because we want to
779+
// close it in resource_listener_dealloc_callback
780+
// Or some pid could still start the stopped listener with nif handle
781+
MsQuic->ListenerStop(l_ctx->Listener);
782+
}
773783
TP_CB_3(end, (uintptr_t)l_ctx->Listener, 0);
774784
}
775785

786+
787+
/*
788+
** Listener NIF handle, end of world...
789+
*/
776790
void
777791
resource_listener_dealloc_callback(__unused_parm__ ErlNifEnv *env, void *obj)
778792
{

0 commit comments

Comments
 (0)