Skip to content

launch.c: Close the file descriptors after being forwarded

Ludovico de Nittis requested to merge wip/denittis/close-fd into master

g_unix_fd_list_append() duplicates the given FD, so we need to close our copy, otherwise it might be left indefinitely open.

Also as soon as we bounce them out with D-Bus, we need need to de-reference the FD list to avoid keeping them open.


This fixes the issue https://gitlab.steamos.cloud/steam/steamlinuxruntime/-/issues/5

https://github.com/flatpak/flatpak-xdg-utils is also affected by the same bug. After this one gets merged I'll prepare a similar patch for upstream too.

To test it I created a simple bash script, similar to _start-container-in-background that contained:

./pressure-vessel-launch --socket=/tmp/pvs/socket --forward-fd=3 -- /tmp/pvs/launch 3>"/tmp/pvs/fifo" &
log "Started in backgroud"
socket=$(
    while read -r line; do
        log "Received $line"
    done < "/tmp/pvs/fifo"
)
log "OK, ready to exit"
exit 0

And a launch.c file with:

int
main (int argc,
      char *argv[])
{
  sleep (2);
  FILE *info_fh = fdopen (3, "w");
  g_warning ("Opened FD 3");
  fprintf (info_fh, "hello\n");
  fflush (info_fh);
  sleep (2);
  g_warning ("Closing FD 3");
  g_clear_pointer (&info_fh, fclose);
  sleep (30);
}

By launching the bash script the expected behaviour was to see the script to end in just a few seconds. If instead it waited the 30 seconds of launch.c then the FD was not closed as expected.

/cc @smcv

Merge request reports