Fd leaks and cloexec#508
Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 3 commits intoMar 11, 2026
Merged
Conversation
In library mode, ddprof_start_profiling() fork+execs the daemon. Every FD without CLOEXEC in the parent leaks into the child process. Changes: - create_elf.cc: use UniqueFd for RAII cleanup (fixes FD leak on error paths), add O_CLOEXEC, fix "prox" typo in log message - ringbuffer_utils.cc: close mapfd on ftruncate/eventfd error paths, add EFD_CLOEXEC to eventfd() - ipc.cc: use accept4() with SOCK_CLOEXEC (parent socket already uses SOCK_CLOEXEC but accepted connections didn't inherit it), add SFD_CLOEXEC to signalfd() - daemonize.cc: add O_CLOEXEC to pipe2() - ddprof_module_lib.cc: add O_CLOEXEC to open() calls (already uses UniqueFd for RAII) Fixes #497
The write end is passed to ddprof via --pipefd and must survive the execve() in exec_ddprof() so ddprof can write back the socket path. Setting O_CLOEXEC caused it to be closed on exec, breaking profiler startup in library mode.
r1viollet
commented
Mar 6, 2026
| const int exe_fd = ::open("/proc/self/exe", O_RDONLY); | ||
| if (exe_fd == -1) { | ||
| LG_WRN("Failed to open /prox/self/exe"); | ||
| const UniqueFd exe_fd{::open("/proc/self/exe", O_RDONLY | O_CLOEXEC)}; |
Collaborator
Author
There was a problem hiding this comment.
cloexec is probably useless here
nsavoire
approved these changes
Mar 6, 2026
Collaborator
Author
|
/merge |
|
View all feedbacks in Devflow UI.
The expected merge time in
|
gh-worker-dd-mergequeue-cf854d
Bot
deleted the
r1viollet/fix/fd-leaks-and-cloexec
branch
March 11, 2026 14:35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Refer to this PR
Limitation
These fixes improve correctness and are good defensive hygiene. However, they are unlikely to resolve FD accumulation in long-running services using library mode: the CLOEXEC additions are all in ddprof's own process (the daemon), not in the target process or the injected library. FDs in the target process that would leak into a restarted ddprof daemon across fork+exec cycles would need to be investigated on the library side (dd_profiling.cc).