coro: Extend coro stack for SIMD operated yyjson#11586
coro: Extend coro stack for SIMD operated yyjson#11586
Conversation
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
📝 WalkthroughWalkthroughA new Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
include/fluent-bit/flb_coro.h (1)
80-84: Consider guarding manualFLB_CORO_STACK_SIZEagainst undersizing in SIMD builds.If
FLB_CORO_STACK_SIZEis defined, the SIMD multiplier path is skipped entirely. Adding a compile-time check (or at least a prominent warning comment) for a minimum safe size would prevent reintroducing the same crash via custom builds.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@include/fluent-bit/flb_coro.h` around lines 80 - 84, When FLB_CORO_STACK_SIZE is user-defined it bypasses the SIMD-aware default; add a compile-time guard that compares FLB_CORO_STACK_SIZE against the computed safe minimum ((3 * STACK_FACTOR * SIMD_STACK_FACTOR * PTHREAD_STACK_MIN) / 2) and emit a `#error` or `#warning` if the user value is smaller, so update the FLB_CORO_STACK_SIZE / FLB_CORO_STACK_SIZE_BYTE logic to validate FLB_CORO_STACK_SIZE using that expression (referencing FLB_CORO_STACK_SIZE, FLB_CORO_STACK_SIZE_BYTE, SIMD_STACK_FACTOR, STACK_FACTOR, PTHREAD_STACK_MIN) and fail/notify the build when undersized.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@include/fluent-bit/flb_coro.h`:
- Around line 80-84: When FLB_CORO_STACK_SIZE is user-defined it bypasses the
SIMD-aware default; add a compile-time guard that compares FLB_CORO_STACK_SIZE
against the computed safe minimum ((3 * STACK_FACTOR * SIMD_STACK_FACTOR *
PTHREAD_STACK_MIN) / 2) and emit a `#error` or `#warning` if the user value is
smaller, so update the FLB_CORO_STACK_SIZE / FLB_CORO_STACK_SIZE_BYTE logic to
validate FLB_CORO_STACK_SIZE using that expression (referencing
FLB_CORO_STACK_SIZE, FLB_CORO_STACK_SIZE_BYTE, SIMD_STACK_FACTOR, STACK_FACTOR,
PTHREAD_STACK_MIN) and fail/notify the build when undersized.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 19ad2cd5-d306-44c5-b55d-374bf85efcde
📒 Files selected for processing (1)
include/fluent-bit/flb_coro.h
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 152debaa7b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| #define FLB_CORO_STACK_SIZE_BYTE FLB_CORO_STACK_SIZE | ||
| #else | ||
| #define FLB_CORO_STACK_SIZE_BYTE ((3 * STACK_FACTOR * PTHREAD_STACK_MIN) / 2) | ||
| #define FLB_CORO_STACK_SIZE_BYTE ((3 * STACK_FACTOR * SIMD_STACK_FACTOR * PTHREAD_STACK_MIN) / 2) |
There was a problem hiding this comment.
Use an absolute minimum for SIMD coroutine stacks
On musl-based builds PTHREAD_STACK_MIN is 2048, so this formula produces only 6144 bytes when FLB_HAVE_SIMD is enabled. That is still far below the ~38 KiB yyjson_read_opts() frame called out in the commit message, and config->coro_stack_size is initialized from FLB_CORO_STACK_SIZE_BYTE in src/flb_config.c:394, so Alpine/musl packages will continue to hit the same coroutine overflow. Please clamp the SIMD path to a real byte floor instead of another PTHREAD_STACK_MIN multiple.
Useful? React with 👍 / 👎.
| #define FLB_CORO_STACK_SIZE_BYTE FLB_CORO_STACK_SIZE | ||
| #else | ||
| #define FLB_CORO_STACK_SIZE_BYTE ((3 * STACK_FACTOR * PTHREAD_STACK_MIN) / 2) | ||
| #define FLB_CORO_STACK_SIZE_BYTE ((3 * STACK_FACTOR * SIMD_STACK_FACTOR * PTHREAD_STACK_MIN) / 2) |
There was a problem hiding this comment.
Limit the larger stack to yyjson callers
config->coro_stack_size is a global default that every co_create(config->coro_stack_size, ...) site consumes, including input collectors (include/fluent-bit/flb_input.h:666), scheduler timers (src/flb_scheduler.c:499), and output flush coroutines (include/fluent-bit/flb_output.h:1155). With this multiplier, any FLB_SIMD build now reserves 2x heap for every coroutine even when that coroutine never enters the yyjson pack path that motivated the fix, so deployments with many concurrent flush/retry coroutines take a measurable memory regression just by enabling SIMD.
Useful? React with 👍 / 👎.
Root Cause Analysis
Enabling SIMD causes the default JSON pack APIs (
flb_pack_json()andflb_pack_json_recs()) to be routed through the extensible (_ext) path, which selects the YYJSON backend.The YYJSON parser (
yyjson_read_opts()) allocates a very large stack frame (~38 KiB) at function entry:In the current execution context (libco coroutine stack), this exceeds the available stack space. As a result, the stack pointer (
rsp) crosses into an unmapped/guard region immediately after the allocation. The crash occurs at the first local variable write:This happens before any JSON parsing logic executes, so the failure is not related to input data or parsing correctness.
Key Observations
Function arguments are passed correctly (verified via registers:
rdi,rsi,rdx, etc.).The crash occurs in the function prologue, not in parsing logic.
yyjson_read_opts()requires significantly more stack than the legacy pack path.Increasing coroutine stack size (e.g., via
STACK_FACTOR) did not resolve the issue, indicating:Why This Is a Regression
Before this change:
After this change (with SIMD enabled):
This rerouting exposes existing callers (e.g., output plugins like Kinesis) to a backend whose stack requirements are incompatible with the coroutine stack constraints.
Conclusion
The issue is not caused by invalid inputs or API misuse, but by:
Enter
[N/A]in the box, if an item is not applicable to your change.Testing
Before we can approve your change; please submit the following in a comment:
If this is a change to packaging of containers or native binaries then please confirm it works for all targets.
ok-package-testlabel to test for all targets (requires maintainer to do).Documentation
Backporting
Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.
Summary by CodeRabbit