xdebug / vscode-php-debug

PHP Debug Adapter for Visual Studio Code 🐞⛔
MIT License
768 stars 175 forks source link

PHP Server runs extremely slowly when xdebug is installed #913

Closed HmCody closed 1 year ago

HmCody commented 1 year ago

This may actually be a bug with xdebug, not PHP Debug, but the fix was to ignore something in your installation instructions, so I thought I would report it.

When I install PHP Debug as recommended, running any WordPress site on my local machine becomes painfully slow. This first occurred using PHP 7.4 and when I tried totally uninstalling VSC and reinstalling using PHP 8.2, the problem immediately cropped up again as soon as I installed xdebug. This was before I had even installed PHP Debug on VSC. I found this thread on Stack Overflow and the Answer by Janne suggested commenting out the;xdebug.start_with_request = yes line. Once I did that, everything returned to a normal pace, and I can now use PHP Debug using "Launch with Web Browser."

Not sure there is a lot you can do to fix the problem, but maybe adding a note to the installation instructions to indicate that there may be a problem would help some people avoid beating their head into a wall? Thanks!

PHP version: 8.2.8 and 7.4.33 Xdebug version: 3.2.1.1 VS Code extension version: 1.32.1

Your launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "hostname": "local",
            "pathMappings": {
              "/var/www": "${workspaceFolder}"
            }
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9003,
            "runtimeArgs": [
                "-dxdebug.start_with_request=yes"
            ],
            "env": {
                "XDEBUG_MODE": "debug,develop",
                "XDEBUG_CONFIG": "client_port=${port}"
            }
        },
        {
            "name": "Launch Built-in web server",
            "type": "php",
            "request": "launch",
            "runtimeArgs": [
                "-dxdebug.mode=debug",
                "-dxdebug.start_with_request=yes",
                "-S",
                "localhost:4044"
            ],
            "program": "",
            "cwd": "${workspaceRoot}",
            "port": 9003,
            "serverReadyAction": {
                "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
                "uriFormat": "http://localhost:%s",
                "action": "openExternally"
            }
        }
    ]
}

Xdebug php.ini config:

zend_extension = xdebug
xdebug.mode = debug
;xdebug.start_with_request = yes

Xdebug logfile (from setting xdebug.log in php.ini): VS Code extension logfile (from setting "log": true in launch.json):

Code snippet to reproduce:

zobo commented 1 year ago

Hi, sorry for the late reply, I was on vacation.

I'm not sure what exactly your setup is and what you mean by Launch with Web Browser. Are you running PHP with a web server (nginx + php-fpm)? And what configuration in VS Code are you using? Perhaps Listen for Xdebug?

If xdebug.start_with_request = yes is set, PHP/Xdebug will try to connect to the debugger (VS Code + PHP Debug) each time a script starts running (be it in web server or CLI) and depending on your OS the connation can be rejected right away or due to some firewall settings can take a long time to timeout.

hostname is generally not needed as by default it will listen on all local addresses.

Also, as I have developed some inhouse WordPress plugins, good thing to know is that WordPress runs an insane amount of code for each request, and debugging can slow that a bit down. But I do not think this is your case.

You can try to enable xdebug logging as per instructions and see if there is a long timeout time between Xdebug trying to connect to the debugger and giving up.

HmCody commented 1 year ago

Duh! Not "Launch with Web Browser," "Launch Built-in web server." I apparently didn't have enough coffee when I was writing the issue.

I am not really trying to fix anything at this point, since PHP Debug is working just fine with xdebug.start_with_request = yes commented out in php.ini. Or at least it does with the "Launch Built-in web server" configuration. If I try "Listen for Xdebug," I get this message from Visual Studio Code: "getaddrinfo ENOTFOUND local" with the options to "Open 'launch.json' or "Cancel." Since I only need to run code from the browser for a local installation, this isn't a problem for me.

What was a problem was everything running slowly when xdebug.start_with_request = yes was enabled in php.ini. This happened even if I wasn't debugging, or even if the PHP Debug plugin wasn't installed. So it isn't a problem with PHP Debug itself, just with the part of the Installation instructions that talks about enabling remote debugging in php.ini that indicates that xdebug.start_with_request = yes should be added to php.ini. Since PHP Debug seems to work without it, it might be helpful if the instructions mentioned that the line could cause issues which might be solved by disabling it.

Hope your vacation was fun and restful!

zobo commented 1 year ago

The getaddrinfo ENOTFOUND local error comes from this configuration part

        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "hostname": "local",  <-- remove this
            "pathMappings": {
              "/var/www": "${workspaceFolder}"
            }
        },

Great to hear you have your dev environment working, I was just trying to understand where the problem came from. I'll close the issue.