Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Register
  • Sign in
  • G GLib
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 853
    • Issues 853
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 54
    • Merge requests 54
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Releases
  • Packages and registries
    • Packages and registries
    • Container Registry
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • GNOMEGNOME
  • GLib
  • Merge requests
  • !3076

gspawn: Ignore invalid FDs when using safe_fdwalk()

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Philip Withnall requested to merge pwithnall/glib:valgrind-fd-fun into main Nov 16, 2022
  • Overview 6
  • Commits 2
  • Pipelines 8
  • Changes 1

In safe_closefrom(), we thought it would be OK to assert that an FD being closed is valid, when using safe_fdwalk(), as it only walks over known-valid FDs.

However, there are two situations where that might not be true:

  1. The FD is closed from another thread between being chosen for closing by safe_fdwalk() and g_close() actually being called on it. This is unlikely and I haven’t seen it in practice, but it is possible because using safe_fdwalk() circumvents the normal static refcounting of FDs. See discussion below
  2. The program is being run under valgrind. Valgrind opens some FDs for its own use which are ≥1024, and it emulates a lowered soft limit on FDs. So if we were to use safe_fdwalk_with_invalid_fds() it would see the lowered soft limit and not try to close valgrind’s internal FDs. However, safe_fdwalk() looks at /proc, which valgrind does not emulate, so it sees the secret valgrind internal FDs, and then tries to close them. Valgrind doesn’t like this, prints “Warning: invalid file descriptor 1024 in syscall close()” and returns EBADF. That return value causes g_close() to warn about faulty FD refcounting, and that causes unit test failures.

Fix that by relaxing our assumptions about FD validity: use the close_func_with_invalid_fds() call back for closing FDs from safe_fdwalk(), rather than using close_func(). That will ignore EBADF return values.

This should fix valgrind failures like this one: https://gitlab.gnome.org/GNOME/glib/-/jobs/2389977

Related prior art: https://bugs.freedesktop.org/show_bug.cgi?id=99839

Signed-off-by: Philip Withnall pwithnall@endlessos.org

Edited Nov 18, 2022 by Philip Withnall
Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: valgrind-fd-fun