Skip to content

Fd leaks and cloexec#508

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 3 commits into
mainfrom
r1viollet/fix/fd-leaks-and-cloexec
Mar 11, 2026
Merged

Fd leaks and cloexec#508
gh-worker-dd-mergequeue-cf854d[bot] merged 3 commits into
mainfrom
r1viollet/fix/fd-leaks-and-cloexec

Conversation

@r1viollet

@r1viollet r1viollet commented Mar 6, 2026

Copy link
Copy Markdown
Collaborator

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).

xroche and others added 3 commits March 6, 2026 13:53
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.
Comment thread src/create_elf.cc
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)};

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

cloexec is probably useless here

@r1viollet

Copy link
Copy Markdown
Collaborator Author

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Mar 11, 2026

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-03-11 14:16:58 UTC ℹ️ Start processing command /merge


2026-03-11 14:17:03 UTC ℹ️ MergeQueue: pull request added to the queue

The expected merge time in main is approximately 22m (p90).


2026-03-11 14:35:06 UTC ℹ️ MergeQueue: This merge request was merged

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.

3 participants