Skip to content

in_tail: fix DB.compare_filename mismatch when monitoring symlinks#11544

Open
zdyj3170101136 wants to merge 1 commit intofluent:masterfrom
zdyj3170101136:fix/tail-db-compare-filename-symlink
Open

in_tail: fix DB.compare_filename mismatch when monitoring symlinks#11544
zdyj3170101136 wants to merge 1 commit intofluent:masterfrom
zdyj3170101136:fix/tail-db-compare-filename-symlink

Conversation

@zdyj3170101136
Copy link

@zdyj3170101136 zdyj3170101136 commented Mar 12, 2026

When DB.compare_filename is enabled and the monitored path is a symbolic link (e.g. glog-style logging), the filename comparison in flb_tail_target_file_name_cmp() always fails on restart.

The database stores the symlink path (file->name), but the comparison checks against file->real_name which is resolved via the file descriptor to the actual target path. For symlinks such as:

app.INFO -> app.INFO.20250303-120000.12345

the basenames differ ("app.INFO" vs "app.INFO.20250303-120000.12345"), so db_file_exists() returns FLB_FALSE. The existing entry is then deleted as stale and re-inserted with offset 0, causing a full re-read of the file and duplicate log ingestion.

Fix this by adding a fallback comparison against file->name (the original glob-matched path) when the real_name comparison fails. This correctly handles symlinks while still protecting against inode reuse for regular files.


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
    • Enhanced symlink handling in file comparison operations to provide more robust fallback behavior across platforms, improving reliability in edge cases.

When DB.compare_filename is enabled and the monitored path is a
symbolic link (e.g. glog-style logging), the filename comparison
in flb_tail_target_file_name_cmp() always fails on restart.

The database stores the symlink path (file->name), but the comparison
checks against file->real_name which is resolved via the file descriptor
to the actual target path.  For symlinks such as:

  app.INFO -> app.INFO.20250303-120000.12345

the basenames differ ("app.INFO" vs "app.INFO.20250303-120000.12345"),
so db_file_exists() returns FLB_FALSE.  The existing entry is then
deleted as stale and re-inserted with offset 0, causing a full
re-read of the file and duplicate log ingestion.

Fix this by adding a fallback comparison against file->name (the
original glob-matched path) when the real_name comparison fails.
This correctly handles symlinks while still protecting against
inode reuse for regular files.

Signed-off-by: jie.yang <zdyj3170101136@gmail.com>
@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2026

📝 Walkthrough

Walkthrough

Adds a fallback mechanism to flb_tail_target_file_name_cmp function to handle symlink comparison edge cases. When the initial basename comparison against the file's real name fails and a symlink name is available, the function re-performs the comparison using the symlink path, supporting both Windows and non-Windows environments.

Changes

Cohort / File(s) Summary
Symlink Fallback in Tail File Comparison
plugins/in_tail/tail_file.h
Adds fallback logic to re-compare file names using symlink path when initial basename comparison fails. Implements conditional re-check with symlink name using strcmp on base names for both Windows and non-Windows branches.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

  • #11543 — Directly addresses the symlink mismatch issue by implementing a fallback comparison mechanism in flb_tail_target_file_name_cmp that re-evaluates using the stored symlink name when initial comparison fails.

Suggested labels

backport to v4.0.x

Suggested reviewers

  • edsiper

Poem

🐰 A symlink's twist, once caused dismay,
But now our names will find their way,
With fallback paths both old and new,
The comparison sees both sides through! 🔗✨

🚥 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 clearly and concisely summarizes the main change: fixing a DB.compare_filename mismatch when monitoring symlinks in the in_tail plugin, which matches the core issue and solution described in the PR objectives.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 39ad31904a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@plugins/in_tail/tail_file.h`:
- Around line 103-109: The code compares base names using flb_strdup(file->name)
but when that allocation fails it leaves ret set to the previous compare result,
causing OOM to be treated as a mismatch; change the logic around flb_strdup to
detect NULL and propagate an allocation error (set ret = -1 or return -1 per the
function contract) instead of continuing the comparison. Specifically, in the
block that calls flb_strdup(file->name) (variables: name_b, base_b, flb_strdup,
_stricmp, ret, file->name) ensure that if flb_strdup returns NULL you set ret =
-1 (or return -1 immediately) and do not call basename/_stricmp; mirror the same
change for the second occurrence (lines 126-132) so allocation failures are
propagated consistently.
- Around line 98-110: The fallback comparison that uses file->name should be
limited to symlink-backed files only; modify the blocks in db_file_exists() and
flb_tail_file_is_rotated() where you currently check "if (ret != 0 &&
file->name)" to also require "file->is_link == FLB_TRUE" (i.e. change to "if
(ret != 0 && file->is_link == FLB_TRUE && file->name)"), and apply the same
guard in the analogous fallback at the other location (the block around lines
121-133) so the basename comparison only runs for symlinks.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2a9e0858-0a8a-456d-a5f3-c5660fcd1115

📥 Commits

Reviewing files that changed from the base of the PR and between 405fed0 and 39ad319.

📒 Files selected for processing (1)
  • plugins/in_tail/tail_file.h

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.

1 participant