@@ -145,6 +145,22 @@ static int flb_azure_kusto_resources_clear(struct flb_azure_kusto_resources *res
145145 resources -> identity_token = NULL ;
146146 }
147147
148+ /* Also clean up any old resources pending destruction */
149+ if (resources -> old_blob_ha ) {
150+ flb_upstream_ha_destroy (resources -> old_blob_ha );
151+ resources -> old_blob_ha = NULL ;
152+ }
153+
154+ if (resources -> old_queue_ha ) {
155+ flb_upstream_ha_destroy (resources -> old_queue_ha );
156+ resources -> old_queue_ha = NULL ;
157+ }
158+
159+ if (resources -> old_identity_token ) {
160+ flb_sds_destroy (resources -> old_identity_token );
161+ resources -> old_identity_token = NULL ;
162+ }
163+
148164 resources -> load_time = 0 ;
149165
150166 return 0 ;
@@ -598,6 +614,29 @@ int azure_kusto_load_ingestion_resources(struct flb_azure_kusto *ctx,
598614 parse_ingestion_identity_token (ctx , response );
599615
600616 if (identity_token ) {
617+ /* Deferred cleanup: destroy resources from two refresh cycles ago,
618+ * then move current resources to 'old' before assigning new ones.
619+ * This avoids use-after-free when other threads may still be using
620+ * the current resources during high-volume operations. */
621+ if (ctx -> resources -> old_blob_ha ) {
622+ flb_upstream_ha_destroy (ctx -> resources -> old_blob_ha );
623+ flb_plg_debug (ctx -> ins , "clearing up old blob HA" );
624+ }
625+ if (ctx -> resources -> old_queue_ha ) {
626+ flb_upstream_ha_destroy (ctx -> resources -> old_queue_ha );
627+ flb_plg_debug (ctx -> ins , "clearing up old queue HA" );
628+ }
629+ if (ctx -> resources -> old_identity_token ) {
630+ flb_sds_destroy (ctx -> resources -> old_identity_token );
631+ flb_plg_debug (ctx -> ins , "clearing up old identity token" );
632+ }
633+
634+ /* Move current to old */
635+ ctx -> resources -> old_blob_ha = ctx -> resources -> blob_ha ;
636+ ctx -> resources -> old_queue_ha = ctx -> resources -> queue_ha ;
637+ ctx -> resources -> old_identity_token = ctx -> resources -> identity_token ;
638+
639+ /* Assign new resources */
601640 ctx -> resources -> blob_ha = blob_ha ;
602641 ctx -> resources -> queue_ha = queue_ha ;
603642 ctx -> resources -> identity_token = identity_token ;
0 commit comments