Xdebug Server is not listening on all specified ports when starting scrippt debugger
In PhpStorm 2022.1.2 (but I saw the same behavior in 2021), the Xdebug Server seems to be ignoring the provided ports on which it should be listening.
I have a default xdebug3 configuration, so the port is 9003. In PhpStorm, I left the default setting for ports: 9000,9003. If I try to debug a script in PhpStorm, I can see in logs it tries to make a connection on port 9003. But at the same time, I see PhpStorm only listens on port 9000. Even though the configuration suggests something else. The logs confirm that:
2022-06-09 19:26:11,891 [ 117329] INFO - #c.j.p.r.RemoteTransfer - Helpers upload successfully completed
2022-06-09 19:26:11,891 [ 117329] INFO - #c.i.d.PerformanceWatcherImpl - Helpers uploaded took 155ms; general responsiveness: ok; EDT responsiveness: ok
2022-06-09 19:26:11,898 [ 117336] INFO - #c.i.s.i.s.sshj - Will request `sftp` subsystem
2022-06-09 19:26:12,254 [ 117692] INFO - #c.j.p.u.c.ServerConnection - Stopped listening on port 9000
2022-06-09 19:26:18,760 [ 124198] INFO - #c.i.c.ComponentStoreImpl - Saving Project(name=www, containerState=COMPONENT_CREATED, componentStore=/home/alex/Projects/course-xdebug/projects/vagrant/www)RunManager took 32 ms
2022-06-09 19:26:18,766 [ 124204] INFO - STDOUT - []
There is not a single mention of port 9003.
If I change the order in which ports are defined: so "9003,9000", everything works. PhpStorm listens on port 9003, and a connection is established.
If I leave the configuration as 9000,9003 BUT at the same time toggle "Start Listening for PHP Debug Connections" (which is not needed when debugging scripts), the connection gets established correctly. Logs also tell the same story:
2022-06-09 19:31:40,655 [ 446093] INFO - #c.j.p.u.c.ServerConnection - Stopped listening on port 9000
2022-06-09 19:34:30,732 [ 616170] INFO - #c.j.p.u.c.ServerConnection - Stopped listening on port 9003
The question is: is this an expected behavior? I can't recollect seeing anything related to it in the documentation, and this behavior feels odd.
请先登录再写评论。
Hi there,
Yes, when you use Debug on "PHP Script" type of Run/Debug Configuration it passes the first port as one of the -d parameters to PHP executable (to configure Xdebug). Since such a param will overwrite your port from php.ini, only listening on the first port should be just fine. You can see the full command that the IDE uses on the Console tab of the Debug toolwindow, e.g.
E:\Projects\php\php80\php.exe -dxdebug.mode=debug -dxdebug.client_port=9003 -dxdebug.client_host=127.0.0.1 E:\Projects\web\_idetest\test.php
Sadly I cannot locate any tickets/doc pages where such behaviour is explained. Only my own comment in https://youtrack.jetbrains.com/issue/WI-64944
But when you use the "phone handle" icon ("Run | Start Listening for PHP Debug Connections") then it will listen on all configured ports (as it cannot configure PHP launched by a web server).
It depends what you want to debug. You can use this to debug remote CLI scripts where you cannot launch PHP from the IDE. Or even a local one that is executed by another process. Most common example: cron tasks.
The problem for me was that PhpStor was detecting the wrong IP of my IDE. So I changed it in xdebug.ini and disabled passing parameters to CLI. And then it doesn't work anymore because the default port is 9003, and Xdebug Server listens only on port 9000.
Not that big of a deal, but it looks like a bug to me. I would expect it to always listen on all specified ports.
An alternative here (when PhpStorm uses the wrong IP) is to add custom -d parameter that will pass the right IP to Xdebug. Such extra params go after what the IDE inserts and therefore will take over them.
Sure, there are various ways to work around that issue - I totally agree. All I'm trying to establish here is whether this is a bug or not well-documented feature ;)