Skip to content

Commit 98ad84b

Browse files
committed
in_kubernetes_events: fix bytes_consumed to always report full payload
The bytes_consumed parameter must always report the full new payload size to the HTTP client, even when buffering incomplete JSON data. Reporting 0 when nothing parses causes the HTTP client to re-send the same data, leading to infinite accumulation and memory corruption. This fixes the test failures where buffers grew from 400 to 1200 to 2376 bytes instead of properly accumulating 400+400+376=1176 bytes. Signed-off-by: Jesse Awan <jesse.awan@sap.com>
1 parent d20f930 commit 98ad84b

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

plugins/in_kubernetes_events/kubernetes_events.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -852,23 +852,12 @@ static int process_http_chunk(struct k8s_events* ctx, struct flb_http_client *c,
852852
}
853853

854854
/*
855-
* Calculate how many bytes of the NEW payload were consumed.
856-
* working_buffer = old_tail + new_payload
857-
* old_len = length of old tail, new_len = length of new payload
858-
* consumed_new = bytes of NEW payload consumed = total_consumed - old_len
855+
* Always report that we consumed the full NEW payload.
856+
* Even if we buffered data without parsing, we ACCEPTED the bytes
857+
* from the HTTP layer, so we must tell it to discard them.
858+
* Reporting 0 would cause the HTTP client to re-send the same data.
859859
*/
860-
size_t new_len = c->resp.payload_size;
861-
size_t old_len = (total_len > new_len) ? (total_len - new_len) : 0;
862-
size_t consumed_new = 0;
863-
864-
if (total_consumed > old_len) {
865-
consumed_new = total_consumed - old_len;
866-
if (consumed_new > new_len) {
867-
consumed_new = new_len;
868-
}
869-
}
870-
871-
*bytes_consumed = consumed_new;
860+
*bytes_consumed = c->resp.payload_size;
872861
}
873862
else {
874863
/* No buffered data; token_start shows progress in current payload */
@@ -895,8 +884,11 @@ static int process_http_chunk(struct k8s_events* ctx, struct flb_http_client *c,
895884
flb_plg_debug(ctx->ins, "all data processed, no buffering needed");
896885
}
897886

898-
/* Report how much we consumed from current payload */
899-
*bytes_consumed = consumed;
887+
/*
888+
* Always report that we consumed the full payload.
889+
* Even if we buffered data without parsing, we ACCEPTED the bytes.
890+
*/
891+
*bytes_consumed = c->resp.payload_size;
900892
}
901893

902894
if (working_buffer) {

0 commit comments

Comments
 (0)