Skip to content

scheduler: Plug FD leaks on destroy#11977

Merged
edsiper merged 2 commits into
masterfrom
comos0920-plug-fd-leaks-on-lib-mode
Jun 22, 2026
Merged

scheduler: Plug FD leaks on destroy#11977
edsiper merged 2 commits into
masterfrom
comos0920-plug-fd-leaks-on-lib-mode

Conversation

@cosmo0920

@cosmo0920 cosmo0920 commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Previously, we didn't care to dispose FD on destroy.
This causes FD leaks on termination and relaunching frequently with using lib mode.
So, we need to plug FD leaks there.

Closes #11974.


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 scheduler initialization and teardown for improved event channel state management.
    • Ensures proper cleanup of event channel resources during scheduler shutdown.
  • Tests

    • Added comprehensive internal unit tests validating event channel cleanup behavior across scheduler lifecycle.

@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 00334a27-fdbb-4fe1-adf5-1705098c65ac

📥 Commits

Reviewing files that changed from the base of the PR and between b950d65 and 29c102f.

📒 Files selected for processing (3)
  • src/flb_scheduler.c
  • tests/internal/CMakeLists.txt
  • tests/internal/scheduler.c
✅ Files skipped from review due to trivial changes (1)
  • tests/internal/CMakeLists.txt
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/internal/scheduler.c

📝 Walkthrough

Walkthrough

flb_sched_create() now initializes ch_events[] to -1 and zeroes sched->event before channel creation, and passes &sched->event to mk_event_channel_create(). flb_sched_destroy() gains a guarded teardown that calls mk_event_channel_destroy() only when ch_events[0] != -1, then resets both entries. A new unit test verifies the pipe FDs are closed after destruction.

Changes

Scheduler event channel FD leak fix

Layer / File(s) Summary
Guarded create and destroy logic in flb_sched
src/flb_scheduler.c
flb_sched_create() sets ch_events[0] and ch_events[1] to -1 and zeroes sched->event as initial state, and passes &sched->event to mk_event_channel_create(). flb_sched_destroy() conditionally calls mk_event_channel_destroy() only when ch_events[0] != -1, then resets both entries to -1, preventing double-close on repeated init/destroy cycles.
Unit test for event channel cleanup
tests/internal/scheduler.c, tests/internal/CMakeLists.txt
New test test_scheduler_event_channel_cleanup creates a config, event loop, and two schedulers, captures their ch_events pipe FDs, destroys the scheduler and event loop, then asserts flb_pipe_w/flb_pipe_r on each captured FD returns -1. Registered as event_channel_cleanup in TEST_LIST and added to the CMake UNIT_TESTS_FILES list.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 A pipe left open, a leak in the ground,
Now guarded by -1, safe and sound.
On destroy we check before we close,
No dangling FDs—that's how it goes!
Each scheduler cleaned, each handle freed,
The rabbit hops on, with no FDs to bleed. 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'scheduler: Plug FD leaks on destroy' accurately describes the main change: fixing file descriptor leaks in the scheduler destroy operation.
Linked Issues check ✅ Passed The PR implements proper cleanup of ch_events file descriptors in flb_sched_destroy(), directly addressing the FD leak issue #11974 by releasing resources that were previously uncleaned.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing FD leaks in the scheduler: proper event channel destruction and tests verifying the cleanup, with no unrelated modifications.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch comos0920-plug-fd-leaks-on-lib-mode

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.

Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
@cosmo0920 cosmo0920 force-pushed the comos0920-plug-fd-leaks-on-lib-mode branch from b950d65 to 29c102f Compare June 22, 2026 08:17
@edsiper edsiper merged commit 7206173 into master Jun 22, 2026
87 of 90 checks passed
@edsiper edsiper deleted the comos0920-plug-fd-leaks-on-lib-mode branch June 22, 2026 23:00
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.

Using Fluent Bit as a library may lead to file descriptor (FD) leaks

2 participants