Fix FD leaks and add missing CLOEXEC flags#500
Open
xroche wants to merge 1 commit intoDataDog:mainfrom
Open
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 DataDog#497
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?
Fix file descriptor leaks on error paths and add missing
CLOEXECflags across several files. In library mode,ddprof_start_profiling()fork+execs the daemon — every FD withoutCLOEXECin the parent service leaks into the child process.Changes
src/create_elf.ccUniqueFdfor RAII cleanup (fixes FD leak on error paths), addO_CLOEXEC, fix "prox" typosrc/ringbuffer_utils.ccmapfdonftruncate/eventfderror paths, addEFD_CLOEXECtoeventfd()src/ipc.ccaccept4()withSOCK_CLOEXEC(parent socket uses it but accepted connections didn't inherit it), addSFD_CLOEXECtosignalfd()src/daemonize.ccO_CLOEXECtopipe2()src/ddprof_module_lib.ccO_CLOEXECtoopen()calls (already usesUniqueFd)Motivation
For long-running services using ddprof in library mode (e.g., Algolia's 7 production services), leaked FDs accumulate across profiler restarts. The missing
CLOEXECflags cause FDs to leak into the fork+exec'd daemon child on every profiler start.How to test the change?
create_elf.cc: trigger an error path (e.g., corrupt ELF) and verify no FD leak with/proc/self/fdcountringbuffer_utils.cc: triggerftruncatefailure (e.g.,ENOSPCon memfd) and verifymapfdis closedipc.cc: verify accepted connections haveCLOEXECset via/proc/<pid>/fdinfo/<fd>daemonize.cc,ddprof_module_lib.cc: verifyCLOEXECflag on opened FDsClassification
Fixes #497