lib: Recreate the event loop after daemonizing on BSD platforms.
Recreate the event loop after daemonizing on BSD platforms. Because kqueue(2) is not inherited by the child process.
Before reopening, we need to close event channel sockets that were initially created, so as not to leak the sockets. However, the libmonkey interface mk_event_channel_destroy tries to delete sockets from the kqueue. It will fail after daemonizing. We have to call mk_event_channel_destroy before daemonizing.
Fixes #11170
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-testlabel 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
-
New Features
- Exposes public controls to explicitly create and destroy the event-processing loop.
-
Improvements
- More consistent lifecycle management and cleanup of event-processing resources for improved stability.
- Daemon-mode on BSD-family systems now reinitializes event processing after daemonization to reduce runtime issues.
-
Bug Fixes
- Adds a dedicated error code and user-facing message for event-loop creation failures to aid diagnostics.
βοΈ Tip: You can customize this high-level summary in your review settings.
Walkthrough
Introduces exported APIs flb_event_loop_create() and flb_event_loop_destroy(), moves event-loop setup/teardown into those functions, updates flb_create() to call the creator, destroys/recreates the event loop around BSD daemonize, and adds a new error code/message for event-loop creation failures.
Changes
| Cohort / File(s) | Change Summary |
|---|---|
Public headers include/fluent-bit/flb_lib.h |
Added exported declarations: int flb_event_loop_create(flb_ctx_t *ctx); and int flb_event_loop_destroy(flb_ctx_t *ctx);. |
Event-loop implementation src/flb_event_loop.c |
New file implementing flb_event_loop_create() (creates mk_event_loop, allocates ctx->event_channel, creates notification channels) and flb_event_loop_destroy() (destroys channels and event loop with guarded cleanup and resource release). |
Core refactor src/flb_lib.c |
Replaced inline event-loop/channel creation in flb_create() with a call to flb_event_loop_create(ctx); preserved existing error handling and failure return paths. |
Daemonization (BSD) src/fluent-bit.c |
On FreeBSD/NetBSD/OpenBSD/DragonFly, destroy event loop before daemonizing and recreate it after fork; on recreate failure log "[daemon] failed to recreate event loop after daemonizing" and follow existing error path. |
Error codes & messages include/fluent-bit/flb_error.h, src/flb_utils.c |
Added FLB_ERR_EVENT_LOOP_CREATE and mapped it to message "could not create event loop" in error utilities. |
Build system src/CMakeLists.txt |
Added flb_event_loop.c to core source list. |
Sequence Diagram(s)
sequenceDiagram
participant App as Application
participant Lib as libfluent-bit
participant OS as Kernel (BSD)
rect `#DDEFFF`
Note over App,Lib: Startup
App->>Lib: flb_create()
Lib->>Lib: flb_event_loop_create(ctx)
Lib->>OS: create kqueue / event channel
end
rect `#FFEBD9`
Note over App,Lib: Daemonize (BSD)
App->>Lib: flb_main_run(daemonize)
Lib->>Lib: flb_event_loop_destroy(ctx)
Lib->>OS: fork()
OS->>OS: child created (kqueue not inherited)
end
rect `#E6FFE7`
Note over Lib,App: Post-fork recovery
Lib->>Lib: flb_event_loop_create(ctx)
Lib->>OS: recreate kqueue / event channel
Lib->>App: resume startup / signal handling
end
Estimated code review effort
π― 3 (Moderate) | β±οΈ ~25 minutes
- Inspect allocation/cleanup and error paths in
src/flb_event_loop.c(malloc/free, mk_event_channel create/destroy). - Verify BSD daemonize timing, conditional compilation and error logging in
src/fluent-bit.c. - Confirm header exports in
include/fluent-bit/flb_lib.hand CMake inclusion.
Suggested labels
backport to v4.0.x
Suggested reviewers
- edsiper
Poem
π I unhooked the loop before the leap,
Forked the field where kqueues sleep,
I tore it down and built it new,
Now signals find their way on cue,
Hopping home with tidy glue.
Pre-merge checks and finishing touches
β 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%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
β Passed checks (4 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | β Passed | Check skipped - CodeRabbitβs high-level summary is enabled. |
| Title check | β Passed | The title accurately describes the main change: adding event loop recreation after daemonizing on BSD platforms, which is the primary fix implemented across multiple files. |
| Linked Issues check | β Passed | The PR fulfills the core requirement from issue #11170 by implementing event loop recreation after daemonizing on BSD platforms to fix the SIGTERM handling bug. New helper functions and error handling are properly added to support this objective. |
| Out of Scope Changes check | β Passed | All changes are scoped to the stated objective of fixing event loop handling for BSD daemonization. The new error code, helper functions, and platform-specific modifications in flb_main_run are all necessary and directly related to resolving the issue. |
β¨ Finishing touches
- [ ] π Generate docstrings
π§ͺ Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
π Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
π₯ Commits
Reviewing files that changed from the base of the PR and between e3753696d6628cf3d62af52b981696b91471f78c and 1bb6d3b2664818625405a479f5f42ca4e7eebf30.
π Files selected for processing (1)
src/flb_event_loop.c(1 hunks)
π§ Files skipped from review as they are similar to previous changes (1)
- src/flb_event_loop.c
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 @coderabbitai help to get the list of available commands and usage tips.
@coderabbitai review
β Actions performed
Review triggered.
Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.
The patches are really good but we need to add Signed-off line on each of commit. So, could you follow the instructions on this? https://github.com/fluent/fluent-bit/pull/11171/checks?check_run_id=55564201840
I rebased my patch to master and squashed 2 commits into 1 commit. I wrote the category name in the commit message, and also added 'Signed-off-by'.
I updated the category in the commit message to 'lib:'.
I updated my patch to introduce a new error code 'FLB_ERR_EVENT_LOOP_CREATE'. And also change to use flb_free function instead of free.
I recommend implementing a src/flb_event_loop.c with these new function calls which names are prefixed with flb_event_loop_SOMETHING();
I renamed the function names to flb_event_loop_* and moved them to the new file flb_event_loop.c. And added the source file name to CMakeLists.txt.
The prototypes are still written in flb_lib.h. The flb_event_loop.h file already exists and seems to be used for a different purpose.
I decided to update the monkey library to plug descriptor leaks in an error case.
The flb_event_loop_destroy should rely on the fix and be written.