Skip to content

compat: fix basename buffer to be thread-local on Windows#11553

Open
slab-msft wants to merge 1 commit intofluent:masterfrom
slab-msft:fix_compat_thread_race_buf
Open

compat: fix basename buffer to be thread-local on Windows#11553
slab-msft wants to merge 1 commit intofluent:masterfrom
slab-msft:fix_compat_thread_race_buf

Conversation

@slab-msft
Copy link
Contributor

@slab-msft slab-msft commented Mar 13, 2026

Add __declspec(thread) to the static buffer in the Windows basename func to prevent data races in multi-threaded contexts, where concurrent calls could corrupt a shared static buffer.

__declspec(thread)

Issue: Windows basename() race condition causing false file rotation.

basename() in flb_compat.h returns a pointer to a global static char buf[_MAX_PATH].

When two tail inputs run their
worker threads concurrently call basename() via [tail_file.h]:
flb_tail_file_is_rotated() -> flb_tail_target_file_name_cmp()

Thread A sets base_b = basename(name_b) (pointer to shared buf), then Thread B
overwrites that buf before Thread A's _stricmp(base_a, base_b) reads it.
The corrupted comparison returns non-zero -> file falsely detected as rotated.

Symptom: repeated log entries on Windows:
[debug] inode=XXXXXXXXXX rotated: C:\logs\svc\svc_2.log => C:\logs\svc\svc_2.log
followed by the file being purged from DB and re-read from offset 0.

Example Config to reproduce:

[INPUT]
    Name      tail
    Alias     in.tail.app
    Tag       svc.*
    Path      C:\logs\app\*.log
    DB        C:\fluent-bit\db\app.db
    Threaded  On

[INPUT]
    Name      tail
    Alias     in.tail.svc
    Tag       svc.*
    Path      C:\logs\svc\*.log
    DB        C:\fluent-bit\db\svc.db
    Threaded  On

Logs:

[2026/03/13 09:30:43.857854900] [debug] [input:tail:in.tail.svc] inode=1407374883875495 with offset=0 appended as C:\logs\svc\svc.log
[2026/03/13 09:30:44.357575600] [debug] [input:tail:in.tail.svc] inode=1407374883875495 file=C:\logs\svc\svc.log promote to TAIL_EVENT
[2026/03/13 09:39:44.336436200] [debug] [input:tail:in.tail.svc] inode=1407374883875495 rotated: C:\logs\svc\svc.log => C:\logs\svc\svc.log
[2026/03/13 09:39:44.336504700] [debug] [input:tail:in.tail.svc] inode=1407374883875495 rotated C:\logs\svc\svc.log -> C:\logs\svc\svc.log
[2026/03/13 09:39:44.336823100] [ info] [input:tail:in.tail.svc] inode=1407374883875495 handle rotation(): C:\logs\svc\svc.log => C:\logs\svc\svc.log
[2026/03/13 09:39:49.338439700] [debug] [input:tail:in.tail.svc] inode=1407374883875495 purge rotated file C:\logs\svc\svc.log (offset=120909 / size = 120909)
[2026/03/13 09:39:49.338471200] [debug] [input:tail:in.tail.svc] inode=1407374883875495 removing file name C:\logs\svc\svc.log
[2026/03/13 09:39:49.338552700] [debug] [input:tail:in.tail.svc] db: file deleted from database: C:\logs\svc\svc.log
[2026/03/13 09:39:59.322467100] [debug] [input:tail:in.tail.svc] inode=1407374883875495 with offset=0 appended as C:\logs\svc\svc.log
[2026/03/13 09:39:59.326828400] [debug] [input:tail:in.tail.svc] inode=1407374883875495 file=C:\logs\svc\svc.log promote to TAIL_EVENT
[2026/03/13 09:43:04.305067800] [debug] [input:tail:in.tail.svc] inode=1407374883875495 rotated: C:\logs\svc\svc.log => C:\logs\svc\svc.log
[2026/03/13 09:43:04.305146400] [debug] [input:tail:in.tail.svc] inode=1407374883875495 rotated C:\logs\svc\svc.log -> C:\logs\svc\svc.log
[2026/03/13 09:43:04.305202400] [ info] [input:tail:in.tail.svc] inode=1407374883875495 handle rotation(): C:\logs\svc\svc.log => C:\logs\svc\svc.log
[2026/03/13 09:43:09.318865100] [debug] [input:tail:in.tail.svc] inode=1407374883875495 purge rotated file C:\logs\svc\svc.log (offset=120909 / size = 120909)
[2026/03/13 09:43:09.318889600] [debug] [input:tail:in.tail.svc] inode=1407374883875495 removing file name C:\logs\svc\svc.log
[2026/03/13 09:43:09.318964600] [debug] [input:tail:in.tail.svc] db: file deleted from database: C:\logs\svc\svc.log
[2026/03/13 09:43:14.306752900] [debug] [input:tail:in.tail.svc] inode=1407374883875495 with offset=0 appended as C:\logs\svc\svc.log
[2026/03/13 09:43:14.310211200] [debug] [input:tail:in.tail.svc] inode=1407374883875495 file=C:\logs\svc\svc.log promote to TAIL_EVENT


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

  • Refactor
    • Improved thread-safety in an internal compatibility layer to enhance stability in multithreaded scenarios.
    • Reduces risk of concurrency-related errors and intermittent crashes, improving overall reliability without changing public behavior or APIs.

@coderabbitai
Copy link

coderabbitai bot commented Mar 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ec116b2d-9e99-4099-a3b6-1f0d2be68063

📥 Commits

Reviewing files that changed from the base of the PR and between 6fff0e0 and 3904047.

📒 Files selected for processing (1)
  • include/fluent-bit/flb_compat.h

📝 Walkthrough

Walkthrough

A thread-local storage modifier (__declspec(thread)) was added to the static buffer declaration inside the basename function in include/fluent-bit/flb_compat.h; no other logic or public signatures were changed.

Changes

Cohort / File(s) Summary
Thread-local Buffer Declaration
include/fluent-bit/flb_compat.h
Changed static char buf[_MAX_PATH]; to static __declspec(thread) char buf[_MAX_PATH]; in the inline basename implementation to make the buffer thread-local.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

backport to v4.2.x

Suggested reviewers

  • edsiper
  • niedbalski
  • patrick-stephens

Poem

🐰 I nudged a buffer, soft and small,
Made it thread-local — safe for all.
One tiny change, no logic to fight,
A hopeful hop into threaded light. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly describes the main change: adding thread-local storage to the basename buffer on Windows to fix a race condition.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can enable review details to help with troubleshooting, context usage and more.

Enable the reviews.review_details setting to include review details such as the model used, the time taken for each step and more in the review comments.

@slab-msft slab-msft force-pushed the fix_compat_thread_race_buf branch from 4b40170 to 6fff0e0 Compare March 14, 2026 19:30
@slab-msft slab-msft changed the title fix(compat): make basename buffer thread-local on Windows compat: fix basename buffer to be thread-local on Windows Mar 14, 2026
@cosmo0920 cosmo0920 added this to the Fluent Bit v5.0 milestone Mar 17, 2026
@cosmo0920
Copy link
Contributor

This patch looks fine.

Add __declspec(thread) to the static buffer in the Windows
basename func to prevent data races in multi-threaded
contexts, where concurrent calls could corrupt a shared static buffer

Signed-off-by: slabbancz <slabbancz@microsoft.com>
@slab-msft slab-msft force-pushed the fix_compat_thread_race_buf branch from 6fff0e0 to 3904047 Compare March 17, 2026 16:30
@slab-msft slab-msft changed the title compat: fix basename buffer to be thread-local on Windows compat: fix basename buffer to be thread-local on Windows Mar 17, 2026
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.

2 participants