Skip to content

out_kafka: mitigate unbounded data races on kafka - #12817

Merged
edsiper merged 2 commits into
masterfrom
cosmo0920-mitigate-unbounded-data-races-on-kafka
Nov 20, 2026
Merged

edsiper merged 2 commits into
masterfrom
cosmo0920-mitigate-unbounded-data-races-on-kafka

Conversation

@cosmo0920

@cosmo0920 cosmo0920 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Topic lookup-or-create remains mutex-protected because it requires several operations to run together.

Also fixed prefix-name routing and allocation cleanup. The four-worker regression delivered all 192 records correctly, with each dynamic topic registered once.

Verification passed:

cmake --build build -j8
ctest --test-dir build -R '^flb-rt-out_kafka$' --output-on-failure
valgrind --leak-check=full --error-exitcode=1 ./build/bin/flb-rt-out_kafka
/tmp/flb-kafka-venv/bin/python -m pytest tests/integration/scenarios/out_kafka -q
VALGRIND=1 VALGRIND_STRICT=1 /tmp/flb-kafka-venv/bin/python -m pytest tests/integration/scenarios/out_kafka -q

Both integration runs: 31 passed, 47 skipped. Skips concern disabled encoders and unconfigured external tests. Runtime Valgrind reported zero errors. Tests used temporary Python 3.12 because the pinned Protobuf dependency fails on Python 3.14.

Closes #12814.


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:

  • Example configuration file for the change
  • Debug log output from testing the change
  • Attached Valgrind output that shows no leaks or memory corruption was found

If this is a change to packaging of containers or native binaries then please confirm it works for all targets.

  • Run local packaging test showing all targets (including any new ones) build.
  • Set ok-package-test label to test for all targets (requires maintainer to do).

Documentation

  • Documentation required for this feature

Backporting

  • Backport to latest stable release.

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

  • Bug Fixes
    • Improved Kafka topic routing so extracted topic values follow the configured lookup and default-topic behavior. Invalid or unsupported dynamic topic names fall back to the default topic.
    • Improved reliability when multiple workers produce messages, including handling of full queues, retries, and flushes.
  • Tests
    • Added coverage for dynamic-topic routing with multiple workers, including valid and invalid topic names and record delivery counts.

Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
@cosmo0920
cosmo0920 requested a review from edsiper as a code owner November 19, 2026 06:20
@cosmo0920 cosmo0920 added this to the Fluent Bit v5.2.1 milestone Nov 19, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T06:22:08.267177Z cf2a44b PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Kafka output now protects topic operations with a mutex, validates dynamic topic names during lookup, and uses atomic operations for the blocked state in message production and flush checks. Integration tests cover dynamic-topic routing with four workers.

Changes: Kafka output

Layer / File(s) Summary
Synchronized topic lookup and routing
plugins/out_kafka/kafka_config.h, plugins/out_kafka/kafka_config.c, plugins/out_kafka/kafka_topic.c, tests/integration/scenarios/out_kafka/tests/test_out_kafka_001.py
The Kafka context initializes and destroys a topic mutex. Topic creation, default-topic access, and lookup use the mutex. Lookup validates dynamic topic names and falls back to the default topic. Integration tests check routing with four workers.
Atomic blocked-state access
plugins/out_kafka/kafka.c
Message production, raw-payload retries, and flush checks use atomic operations for the blocked state. Extracted topic values no longer create dynamic topics in produce_message.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Possibly related issues

  • fluent/fluent-bit#12814: The issue reports an unlocked race in Kafka topic lookup and registration with multiple workers. This PR synchronizes those topic operations.
Merge Risk: Minimal · up to cf2a4

This change protects Kafka output topic registration with a mutex and makes the blocked flag atomic, so concurrent workers should route records correctly. No concrete merge-blocking risk was found. The author-reported tests pass, but they were not independently rerun here.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "out_kafka: mitigate unbounded data races on kafka" matches the main change. The changeset adds topic-list locking (topics_lock) in kafka_topic.c and kafka_config.c, converts the blocked fla…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "out_kafka: mitigate unbounded data races on kafka" matches the main change. The changeset adds topic-list locking (topics_lock) in kafka_topic.c and kafka_config.c, converts the blocked flag to an atomic uint64_t, and removes dynamic topic creation from produce_message. The title names the plugin and the concurrency fix. It does not name the specific mechanisms (mutex, atomic blocked state), but it still describes the primary change accurately.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1c61e08d-57dd-4567-9df5-f7d7d54c8f97
Commits

Reviewing files that changed from the base of the PR and between 75a9fda and cf2a44b.

Files selected for processing: 5

  • plugins/out_kafka/kafka.c
  • plugins/out_kafka/kafka_config.c
  • plugins/out_kafka/kafka_config.h
  • plugins/out_kafka/kafka_topic.c
  • tests/integration/scenarios/out_kafka/tests/test_out_kafka_001.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@edsiper
edsiper merged commit 00e30ed into master Nov 20, 2026
75 checks passed
@edsiper
edsiper deleted the cosmo0920-mitigate-unbounded-data-races-on-kafka branch November 20, 2026 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

out_kafka: data race on ctx->topics with Workers > 1 corrupts the topic list, causing endless "new topic added" and unbounded memory growth

2 participants