[php-src] Issue #7769: Writing to stderr with a demonized process that has stderr closed sends stderr output out socket instead.

From: Date: Mon, 13 Dec 2021 16:27:34 +0000
Subject: [php-src] Issue #7769: Writing to stderr with a demonized process that has stderr closed sends stderr output out socket instead.
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-238375@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/7769
Comment Author: damianwadley

This is standard behavior for Linux systems.

* stdin, stdout, and stderr are defined as file descriptors (FD) 0, 1, and 2
* Closing a handle allows that FD to be reused
* Opening a new handle, such as with a socket, will always use the lowest FD available

Thus if you close stdout and open a new socket, that will be assigned FD 1, so any attempt to write
to "stdout" will end up writing to that socket. I'd link a good resource explaining
this technique in more detail except I haven't found one yet...

If you want to stop using stdout and stderr then redirect them to /dev/null[^1], but you should
first reconsider whether you actually want to do that: why not use them for output and debugging and
anything else that could be remotely useful, then leave it to _the caller_ to decide whether to
ignore or discard the output? -q/--quiet or -v/--verbose flags to manage
output are very common too.

Moving because I think the fclose docs could benefit from a note about this behavior.

[^1]: You might think that you could open the socket first and close the std handles second, but any
handles opened later will still reuse FDs 1/2 - and that includes socket_accept-ing new
connections, not just fopened files. And that could be a real pain to try to debug. So
don't.


Thread (1 message)

  • damianwadley
« previous php.bugs (#238375) next »