0

When I stop debugging, my PHP daemon script continues to run in the background. How do I make xdebug automatically terminate/kill my PHP script when I stop debugging?

My environment:

  • PHP 8 (cli) with the xdebug extension.
  • Visual Studio Code with the PHP Debug extension.

Visual Studio Code (settings.json):

"launch": {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "PHP Debug",
            "type": "php",
            "request": "launch",
            "program": "${workspaceFolder}/xdebug.php",
            "cwd": "${workspaceFolder}",
            "runtimeArgs": [
                "-dxdebug.log=/tmp/xdebug.log",
            ],
            "env": {
                "XDEBUG_MODE": "debug",
                "XDEBUG_TRIGGER": "true",
                "XDEBUG_CONFIG": "client_port=${port}",
            },
            "internalConsoleOptions": "openOnSessionStart",
        },
    ],
},

Debugging test script (/path/to/xdebug.php):

<?php

sleep(15);

Xdebug log (/tmp/xdebug.log):

[101075] Log opened at 2021-09-03 18:34:25.062284
[101075] [Step Debug] INFO: Connecting to configured address/port: localhost:9003.
[101075] [Step Debug] INFO: Connected to debugging client: localhost:9003 (through xdebug.client_host/xdebug.client_port). :-)
[101075] [Step Debug] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///path/to/xdebug.php" language="PHP" xdebug:language_version="8.0.5" protocol_version="1.0" appid="101075" idekey="true"><engine version="3.0.4"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2021 by Derick Rethans]]></copyright></init>

[101075] [Step Debug] <- feature_set -i 1 -n resolved_breakpoints -v 1
[101075] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_set" transaction_id="1" feature="resolved_breakpoints" success="1"></response>

[101075] [Step Debug] <- feature_set -i 2 -n notify_ok -v 1
[101075] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_set" transaction_id="2" feature="notify_ok" success="1"></response>

[101075] [Step Debug] <- feature_set -i 3 -n extended_properties -v 1
[101075] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_set" transaction_id="3" feature="extended_properties" success="1"></response>

[101075] [Step Debug] <- run -i 4

******** I stopped debugging at 18:34:30 ********

[101075] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="run" transaction_id="4" status="stopping" reason="ok"></response>

[101075] Log closed at 2021-09-03 18:34:40.243542
LogicalException
  • 566
  • 2
  • 7
  • 16
  • Does VSCode send a "stop" or "detach" protocol command? To find out, make a log with the `xdebug.log` setting: https://xdebug.org/docs/all_settings#log and find out :-) – Derick Sep 03 '21 at 08:08
  • Thanks for the reply, @Detick. Nothing in the logs (/tmp/xdebug.log) to indicate a stop/detach command received. It finishes executing `sleep(15);` after I stop debugging then logs "Log closed". – LogicalException Sep 03 '21 at 13:47
  • There shouldn't be a `sleep(15)` in the log - can you show the last ~10 lines of the log? – Derick Sep 03 '21 at 14:44
  • Sorry, `sleep(15);` was the script I was testing xdebug with and is not included in the logs. I've added `/tmp/xdebug.log` in the edited question above where I stopped debugging around 5 seconds before the `Log closed` message. – LogicalException Sep 03 '21 at 16:23
  • `[57177] [Step Debug] <- stop -i 5` — and you say "Nothing in the logs (/tmp/xdebug.log) to indicate a stop/detach command". It has `stop` in it. It is also replied to by Xdebug as `stopped`, and the next log line is `Log closed`. Looks like the script terminated just fine. – Derick Sep 03 '21 at 16:58
  • Sorry, I accidentally posted the log for when my script terminated naturally...I've updated the log now and included a comment for when I requested debugging to be stopped. – LogicalException Sep 03 '21 at 18:39

1 Answers1

1

PHP Debug extension maintainer here.

Due to Xdebug's "synchronous" nature, this isn't possible. You script is a good example.

Xdebug/DBGp can only process one request at time, and no new request can be received until the old has finished. Here the request is "run" and the response is <response ... command="run" ... status="stopping" reason="ok"></response> - received when the script stops.

So in between, you can't send any other commands like stop.

So in other words, you can only stop a scrip if it's in a paused state (i.e. breakpoint).

When PHP Debug receives the Disconnect Request form VS Code it tries to send a stop command - and if it fails to do so in 500ms (connection busy with a different request) it will simply disconnect from the process.

This would allow the PHP process to continue the code.

HOWEVER! If you started the script with the use of program field of launch.json the PHP Debug extension will also KILL the running process.

This isn't obvious here, but there might be some discrepancies in how the script is executed.

zobo
  • 206
  • 1
  • 3