Bug 504177 - FILE DESCRIPTORS banner shows when closing some inherited file descriptors
Summary: FILE DESCRIPTORS banner shows when closing some inherited file descriptors
Status: RESOLVED FIXED
Alias: None
Product: valgrind
Classification: Developer tools
Component: general (other bugs)
Version First Reported In: 3.25.0
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: Julian Seward
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-05-13 20:51 UTC by Mark Wielaard
Modified: 2025-05-13 22:29 UTC (History)
0 users

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mark Wielaard 2025-05-13 20:51:27 UTC
With valgrind 3.25.0 the following shows the FILE DESCRIPTORS banner, when it shouldn't

$ valgrind -q --track-fds=yes cat /dev/null
==1937149== FILE DESCRIPTORS: 1 open (3 inherited) at exit.
==1937149== 

cat (coreutils) closes stdout and stderr before exit. The still open file descriptor is stdin.
Note that it still says 3 inherited.

The problem is that the check whether or not to show the banner is:
(fd_count - inherited == 0)

The fix seems to be to check whether the inherited file descriptors are already closed:

diff --git a/coregrind/m_syswrap/syswrap-generic.c b/coregrind/m_syswrap/syswrap-generic.c
index 82a682a5ce55..81c8fc028d88 100644
--- a/coregrind/m_syswrap/syswrap-generic.c
+++ b/coregrind/m_syswrap/syswrap-generic.c
@@ -987,7 +987,7 @@ void VG_(show_open_fds) (const HChar* when)
    int inherited = 0;
 
    for (i = allocated_fds; i; i = i->next) {
-      if (i->where == NULL)
+      if (i->where == NULL && !i->fd_closed)
          inherited++;
    }
Comment 1 Mark Wielaard 2025-05-13 22:29:39 UTC
commit 29a5fcb3f6371584f89f9b54c793cc0f29802553
Author: Mark Wielaard <mark@klomp.org>
Date:   Wed May 14 00:13:06 2025 +0200

    Don't count closed inherited file descriptors
    
    Programs which close some inherited file descriptors and are run under
    valgrind with -q and --track-fds=yes would still show the FILE
    DESCRIPTORS banner even if there were no non-inherited file
    descriptors still open.
    
    Fix this by not counting already closed inherited file descriptors.
    
    Add a simple testcase to show /usr/bin/cat /dev/null doesn't produce
    any output running under valgrind -q --track-fds=yes.
    
    https://bugs.kde.org/show_bug.cgi?id=504177