@@ -343,14 +343,15 @@ static fr_io_pending_packet_t *pending_packet_pop(fr_io_thread_t *thread)
343343
344344 client = fr_heap_pop (& thread -> pending_clients );
345345 if (!client ) {
346+ fr_assert (thread -> num_pending_packets == 0 );
347+
346348 /*
347349 * 99% of the time we don't have pending clients.
348350 * So we might as well free this, so that the
349351 * caller doesn't keep checking us for every packet.
350352 */
351353 talloc_free (thread -> pending_clients );
352354 thread -> pending_clients = NULL ;
353- thread -> num_pending_packets = 0 ;
354355 return NULL ;
355356 }
356357
@@ -466,6 +467,23 @@ static int _client_free(fr_io_client_t *client)
466467 return 0 ;
467468}
468469
470+ static void client_pending_free (fr_io_client_t * client )
471+ {
472+ size_t num ;
473+
474+ fr_assert (!client -> connection );
475+
476+ if (!client -> pending ) return ;
477+
478+ num = fr_heap_num_elements (client -> pending );
479+
480+ fr_assert (client -> thread -> num_pending_packets >= num );
481+ client -> thread -> num_pending_packets -= num ;
482+
483+ TALLOC_FREE (client -> pending );
484+ }
485+
486+
469487static int connection_free (fr_io_connection_t * connection )
470488{
471489 /*
@@ -932,7 +950,7 @@ static int _client_live_free(fr_io_client_t *client)
932950 fr_assert (!client -> connection );
933951 fr_assert (client -> thread );
934952
935- if (client -> pending ) TALLOC_FREE (client -> pending );
953+ if (client -> pending ) client_pending_free (client );
936954
937955 (void ) fr_trie_remove_by_key (client -> thread -> trie , & client -> src_ipaddr .addr , client -> src_ipaddr .prefix );
938956
@@ -2388,7 +2406,11 @@ static ssize_t mod_write(fr_listen_t *li, void *packet_ctx, fr_time_t request_ti
23882406 inst -> app_io -> common .name , fr_box_ipaddr (client -> src_ipaddr ));
23892407
23902408 client -> state = PR_CLIENT_NAK ;
2391- TALLOC_FREE (client -> pending );
2409+ if (!connection ) {
2410+ client_pending_free (client );
2411+ } else {
2412+ TALLOC_FREE (client -> pending );
2413+ }
23922414 if (client -> table ) TALLOC_FREE (client -> table );
23932415 fr_assert (client -> packets == 0 );
23942416
0 commit comments