Bug 311655 - --log-file=FILE leads to apparent fd leak ("Open file descriptor 3: $LOGFILE")
Summary: --log-file=FILE leads to apparent fd leak ("Open file descriptor 3: $LOGFILE")
Status: RESOLVED FIXED
Alias: None
Product: valgrind
Classification: Developer tools
Component: memcheck (show other bugs)
Version: 3.8.0
Platform: Compiled Sources Linux
: NOR normal
Target Milestone: ---
Assignee: Julian Seward
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-12-13 21:49 UTC by Daniel Richard G.
Modified: 2024-06-17 16:01 UTC (History)
3 users (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Richard G. 2012-12-13 21:49:58 UTC
I am using Valgrind 3.8.1 with the --log-file=FILE option. When the program exits, I see this in the log:

==32582== FILE DESCRIPTORS: 4 open at exit.
==32582== Open file descriptor 3: /path/to/my/log-file.txt
==32582==    <inherited from parent>

The file descriptor used to write the log file should be marked as internal to Valgrind, so that Valgrind does not report it as being (potentially) leaked.

(I have a script that checks for this, and filters out the standard fds 0, 1 and 2.)

Reproducible: Always
Comment 1 eldipa 2022-02-27 14:20:13 UTC
I can confirm the bug in the latest version of valgrind (commit 7f1127140).

There is one catch if you want to reproduce it with the test suite: to make the output reproducible the tests close all the inherited file descriptors which sadly, hides the bug.

To reproduce it, pick the test file "none/tests/fdleak_pipe.c" and comment out the macro "CLOSE_INHERITED_FDS". and recompile with "make check".

Execute:
 ./vg-in-place --track-fds=yes --log-file=foo.log none/tests/fdleak_pipe 
 cat foo.log

This is the output that I've got:

==19349== Memcheck, a memory error detector
==19349== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==19349== Using Valgrind-3.19.0.GIT and LibVEX; rerun with -h for copyright info
==19349== Command: none/tests/fdleak_pipe
==19349== Parent PID: 19347
==19349== 
==19349== 
==19349== FILE DESCRIPTORS: 6 open (3 std) at exit.
==19349== Open file descriptor 5:
==19349==    at 0x4967C57: pipe (syscall-template.S:78)
==19349==    by 0x10916F: main (fdleak_pipe.c:10)
==19349== 
==19349== Open file descriptor 4:
==19349==    at 0x4967C57: pipe (syscall-template.S:78)
==19349==    by 0x10916F: main (fdleak_pipe.c:10)
==19349== 
==19349== Open file descriptor 3: /home/user/forks/valgrind/foo.log
==19349==    <inherited from parent>
==19349== 
==19349== 
==19349== HEAP SUMMARY:
==19349==     in use at exit: 0 bytes in 0 blocks
==19349==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==19349== 
==19349== All heap blocks were freed -- no leaks are possible
==19349== 
==19349== For lists of detected and suppressed errors, rerun with: -s
==19349== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Comment 2 Mark Wielaard 2024-06-17 16:01:57 UTC
commit fbd7596f8342f0b0fbbe088d960da839a8bdb839
Author: Mark Wielaard <mark@klomp.org>
Date:   Sun Jun 16 21:23:08 2024 +0200

    Don't leave fds created with --log-file, --xml-file or --log-socket open
    
    prepare_sink_fd and prepare_sink_socket will create a new file
    descriptor for the output sink. finalize_sink_fd then copies the fd
    to the safe range, so it doesn't conflict with any application fds.
    
    If we created the original fd ourselves, it was a VgLogTo_File or
    VgLogTo_Socket, not VgLogTo_Fd, finalize_sink_fd should close it.
    
    Also close socket when connecting fails in VG_(connect_via_socket).
    
    Add a testcase for --log-file and --xml-file which prints output to
    /dev/stderr
    
    https://bugs.kde.org/show_bug.cgi?id=202770
    https://bugs.kde.org/show_bug.cgi?id=311655
    https://bugs.kde.org/show_bug.cgi?id=488379
    
    Co-authored-by: Alexandra Hájková <ahajkova@redhat.com