Can you please make it possible to detect when the CLI PHP process/window is closed with the "X" button?

From: Date: Wed, 09 Sep 2020 17:05:15 +0000
Subject: Can you please make it possible to detect when the CLI PHP process/window is closed with the "X" button?
Groups: php.windows php.internals.win 
Request: Send a blank email to php-windows+get-31495@lists.php.net to get a copy of this message
For a very long time, I've been desperately trying to detect, from the PHP CLI script itself, the event of somebody closing the window on Windows by clicking the "X" button. I use register_shutdown_function(), but this is not called when the window is closed. It is only run when the script exited/ended on its own. sapi_windows_set_ctrl_handler() is able to handle Ctrl + C and Ctrl + Break closes, but not "X click" closes. (Which is extremely strange, since this is the most common method by far.) The pcntl_* functions are Linux-only. The reason I need to do this is that, unless I can close the database connection, this will be logged by my database every time I close a running PHP CLI script: "LOG:  could not receive data from client: An existing connection was forcibly closed by the remote host." While this doesn't seem to cause any *practical* issue, it bugs me beyond words to see my database log filling up with those errors as I close many PHP CLI windows all day. Even just one is too many. I've gone to great lengths to clean my code from any issues, so this glaring "last" one is really sticking out. Will PHP 8 perhaps have an improved sapi_windows_set_ctrl_handler() which also detects this "X close" event? Frankly, I don't understand why it's a Windows-only function to begin with. I don't like OS-specific functions as one of the major benefits of PHP is that it's (in theory) cross-platform. I wish that sapi_windows_set_ctrl_handler() would work on Linux/Mac and that pcntl_* would work on Windows.

« previous php.windows (#31495) next »