|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-12-30 17:14 UTC] gadelat at gmail dot com
Description: ------------ There is currently no way how to handle CTRL+C with PHP in Windows. This makes it the only scripting language I know that cannot handle this and pretty bad choice for writing cross platform compatible CLI applications. - handler registered via register_shutdown_function is not called for CTR+C - pcntl_signal relies on PCNTL extension which isn't available on Windows Notice that I am trying to avoid word "signal" here as you may argue there are no signals in Windows. Developer still needs a way how to handle CTRL+C press of end user. This is important eg. for running cleanup tasks, as not even PHP's tmpfile() cleanup logic is executed when user aborts program via CTRL+C. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 23:00:01 2025 UTC |
@gadelat at gmail dot com, please check the latest master snapshots. The handling is as simple as the below code function handler($evt) { echo "Got CTRL+C event\n"; exit; } sapi_windows_set_ctrl_handler('handler'); run it and press CTRL+C. An UPGRADING note is to follow yet. Thanks.Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. Thanks for checking. Please show the exact code. There are internal functions that would block the execution, it would probably be same on other systems. Say a function like scanf(). The code like this, which only uses the ZVM instructions should be ok. function handler($evt) { echo "Got CTRL+C event\n"; exit; } sapi_windows_set_ctrl_handler('handler'); while(true) sleep(1); // checking the status I'd need your exact code to tell more. Thanks.Here's reproducer <?php sapi_windows_set_ctrl_handler(function () { echo 'shutting down'; exit(0); }); $server = stream_socket_server('localhost:1337'); stream_set_blocking($server, false); $read = [$server]; $write = []; $except = null; stream_select($read, $write, $except, null, null);Thanks for sending the code. Yes, the select sticks in a blocking call, because it's given the timeout of zero. The CTRL+C handler can't be called, as no further PHP code can be interpreted. My suggestion you do similar to the following instead while (true) { $read = [$server]; $write = []; $except = null; stream_select($read, $write, $except, 0, 5000); } The basic idea - just occasionally return from the blocking calls to give the VM a chance to handle the interrupt. It is one of the Windows specific points, as CTRL+* event doesn't automatically cancel all the I/O nor it does anything else except invoking the handler function. Internally, the VM interrupt is set, but because the select blocks, VM doesn't execute. I'll think about a way to improve this, but I'm pessimistic there can be done much. One could try to close the open descriptors, that might work, but for that one has to record all of them which might be not doable. Thanks.