Manually closing an FD passed in a g_subprocess_launcher

Hi all:

I’m passing an FD to a program launched with g_subprocess_launcher() (using g_subprocess_launcher_take_fd() to pass the FD to the child). Unfortunately, I need that FD to be closed as soon as the program dies. The problem is that the g_subprocess_launcher() object closes the passed FDs automatically only when it is destroyed, but since I’m using Javascript to create the g_subprocess_launcher() object, there is a delay of several seconds between I nullify all the references and the garbage collector truly destroys it. I tried avoid using take_fd() and just pass the FD number directly (because, in theory, the FDs are inherited by the child), and it seems to work… but the documentation of g_subprocess_launcher() says that all the FDs are closed after the FORK and before the EXEC(), so maybe it stops working in a new version. I also tried to keep the take_fd() and use “close(FD)” to close the passed FD, and also works, but I’m afraid that this can result in problems.

So my question is: is it “legal” (in the sense of not producing unwanted side effects) to close from the parent an FD that has been passed with take_fd() to a child, or I must always let the launcher close it during destruction, in which case I should not use take_fd() and just assume that the FD is inherited by the child?

(to give more context, this is the patch I’m working on: https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1450 )

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.