Skip to content

Commit f270724

Browse files
out_azure_kusto: Defer close old resource handles in next cycle
Signed-off-by: ag-ramachandran <ramacg@microsoft.com>
1 parent cc97ad3 commit f270724

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

plugins/out_azure_kusto/azure_kusto.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ struct flb_azure_kusto_resources {
8484

8585
/* used to reload resouces after some time */
8686
uint64_t load_time;
87+
88+
/* Old resources pending cleanup - deferred destruction to avoid use-after-free
89+
* when other threads may still be using them during high-volume operations */
90+
struct flb_upstream_ha *old_blob_ha;
91+
struct flb_upstream_ha *old_queue_ha;
92+
flb_sds_t old_identity_token;
8793
};
8894

8995
struct flb_azure_kusto {

plugins/out_azure_kusto/azure_kusto_conf.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)