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

[email protected] Wed, 9 Sep 2020 19:05:15 +0200 (CEST)
Newsgroups php.windows,php.internals.win
Message-ID <[email protected]>
------=_Part_112137_2112180317.1599671115669
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

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 c=
licking the "X" button.=20

I use `register_shutdown_function()`, but this is not called when the windo=
w 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 conne=
ction, this will be logged by my database every time I close a running PHP =
CLI script:

"LOG:=C2=A0 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 w=
ords to see my database log filling up with those errors as I close many PH=
P CLI windows all day. Even just one is too many. I've gone to great length=
s to clean my code from any issues, so this glaring "last" one is really st=
icking 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_ha=
ndler()` would work on Linux/Mac and that `pcntl_*` would work on Windows.

------=_Part_112137_2112180317.1599671115669--