-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Move input coroutine management variables to TLS #11576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Comment on lines
+581
to
+582
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify the definition in the source file matches the header declaration
rg -n '__thread.*libco_in_param' src/flb_input.c -B1 -A1
# Check how pthread_key_create is called - should be once, not per-coroutine
ast-grep --pattern 'pthread_key_create($_, $_)'Repository: fluent/fluent-bit Length of output: 3133 🏁 Script executed: # Find the function containing line 652 in flb_input.h
head -700 include/fluent-bit/flb_input.h | tail -60 | cat -n
# Check if this is in a per-coroutine or initialization function
rg -n 'flb_input_coro_collect|pthread_key_create' include/fluent-bit/flb_input.h -B5 -A5
# Check how libco_in_param is actually accessed in the code
rg -n 'libco_in_param' src/flb_input.c | head -30Repository: fluent/fluent-bit Length of output: 3082 🏁 Script executed: # Check how libco_in_param_key is used - getspecific/setspecific or direct access
rg -n 'pthread_getspecific|pthread_setspecific|libco_in_param' src/flb_input.c -B2 -A2 | head -60
# Also check include files
rg -n 'libco_in_param' include/fluent-bit/flb_input.h -B2 -A2Repository: fluent/fluent-bit Length of output: 1538 Critical: A Choose one TLS approach:
Additionally, 🤖 Prompt for AI Agents |
||
| 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); | ||
|
|
||
|
Comment on lines
+617
to
+618
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
nl -ba include/fluent-bit/flb_input.h | sed -n '585,670p'Repository: fluent/fluent-bit Length of output: 103 🏁 Script executed: #!/bin/bash
# Read lines 585-670 from flb_input.h
cat -n include/fluent-bit/flb_input.h | sed -n '585,670p'Repository: fluent/fluent-bit Length of output: 3099 🏁 Script executed: #!/bin/bash
# Check the extern declarations and __thread usage
head -100 include/fluent-bit/flb_input.h | cat -nRepository: fluent/fluent-bit Length of output: 4123 🏁 Script executed: #!/bin/bash
# Search for libco_in_param_key declaration
rg "libco_in_param_key" include/fluent-bit/flb_input.h -B 2 -A 2Repository: fluent/fluent-bit Length of output: 908 🏁 Script executed: #!/bin/bash
# Search for libco_in_param_key definition in the source files
rg "libco_in_param_key" src/ --type c -B 1 -A 1 | head -50Repository: fluent/fluent-bit Length of output: 204 Eliminate the unsafe assertion by propagating allocation/setup errors. The assertion at line 617 assumes Required changes:
🤖 Prompt for AI Agents |
||
| coll = params->coll; | ||
| config = params->config; | ||
| coro = params->coro; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,8 +52,8 @@ | |
| #include <fluent-bit/flb_chunk_trace.h> | ||
| #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; | ||
|
Comment on lines
+55
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
rg -n -C3 '\blibco_in_param_key\b|pthread_key_create\s*\(|pthread_key_delete\s*\(|pthread_once\s*\(|pthread_(get|set)specific\s*\(' include/fluent-bit/flb_input.h src/flb_input.cRepository: fluent/fluent-bit Length of output: 2526 Do not make the
🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cosmo0920 Also looking for suggestions here. Removing the key entirely and moving the coroutine params to TLS seems to be the most sensible path, since as the review mentioned, the key is leaked anyway (I observed this during debugging). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| #define protcmp(a, b) strncasecmp(a, b, strlen(a)) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CMakeLists.txtexplicitly supports builds where compiler TLS is unavailable (or forced off withFLB_POSIX_TLS), but these declarations now unconditionally require__thread. That breaks the non-C-TLS build path by makingflb_input.hdepend on compiler TLS syntax in all configurations, so affected targets fail at compile time before the pthread-key fallback can be used. Please switch this to the existingFLB_TLS_*abstraction or gate it with#ifdef FLB_HAVE_C_TLS.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cosmo0920 Open to suggestions here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use the existing TLS abstraction or conditionally compile the declarations like other sources do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If conditional compilation is required, I think that means this approach is broken since the write conflict would still exist on any system without TLS. Should we protect the key variable with a mutex instead?