From c12e0495bb42bc9c740633ccec728a5b62b2cf53 Mon Sep 17 00:00:00 2001 From: Ryan Underwood Date: Tue, 17 Mar 2026 16:11:35 -0500 Subject: [PATCH] engine: move input coroutine management variables to TLS In c98a68e, coroutine state was moved to thread-local storage. However, the variables introduced to manage thread-local coroutine state were not themselves thread-local, which is a problem because flb_input_coro_collect() is not guarded against re-entrancy. This results in a race when multiple threads wake up from polling and run, because they then both read (e.g. in input_pre_cb_collect()) and overwrite (e.g. in flb_input_coro_collect()) the TLS key used to look up the stored coroutine state (libco_in_param_key), without synchronization. There are almost certainly additional bugs here but this at least mitigates the race condition. The error handling logic in input_pre_cb_collect() was removed and replaced with an assert because the recovery path made no sense, and it's not clear there is in fact a sensible recovery path given the code structure. Most likely this is related to #11488 and may be the root cause. Signed-off-by: Ryan Underwood --- include/fluent-bit/flb_input.h | 14 ++++---------- src/flb_input.c | 4 ++-- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/include/fluent-bit/flb_input.h b/include/fluent-bit/flb_input.h index 17d9ffab366..b52df4ccc4a 100644 --- a/include/fluent-bit/flb_input.h +++ b/include/fluent-bit/flb_input.h @@ -578,8 +578,8 @@ struct flb_libco_in_params { struct flb_coro *coro; }; -extern pthread_key_t libco_in_param_key; -extern struct flb_libco_in_params libco_in_param; +extern pthread_key_t __thread libco_in_param_key; +extern struct flb_libco_in_params __thread libco_in_param; void flb_input_coro_prepare_destroy(struct flb_input_coro *input_coro); static FLB_INLINE void input_params_set(struct flb_coro *coro, @@ -614,14 +614,8 @@ static FLB_INLINE void input_pre_cb_collect(void) struct flb_libco_in_params *params; params = pthread_getspecific(libco_in_param_key); - if (params == NULL) { - params = flb_calloc(1, sizeof(struct flb_libco_in_params)); - if (params == NULL) { - flb_errno(); - return; - } - pthread_setspecific(libco_in_param_key, params); - } + flb_bug(params == NULL); + coll = params->coll; config = params->config; coro = params->coro; diff --git a/src/flb_input.c b/src/flb_input.c index ec0c70f0fb2..93378a2cd9b 100644 --- a/src/flb_input.c +++ b/src/flb_input.c @@ -52,8 +52,8 @@ #include #endif /* FLB_HAVE_CHUNK_TRACE */ -struct flb_libco_in_params libco_in_param; -pthread_key_t libco_in_param_key; +struct flb_libco_in_params __thread libco_in_param; +pthread_key_t __thread libco_in_param_key; #define protcmp(a, b) strncasecmp(a, b, strlen(a))