PHP CLI进程在退出时永久挂起(PHP CLI process hangs forever on exit)

编程入门 行业动态 更新时间:2024-10-28 04:29:23
PHP CLI进程在退出时永久挂起(PHP CLI process hangs forever on exit)

我有一个PHP脚本,我在命令行上运行,当脚本应该退出时,它就会永久挂起。 它发生在Windows和Linux上,所以它不是操作系统相关的。

我试着用XDebug调试代码,进入最后的exit语句(已经尝试过,并且没有运气)。 在对某些对象运行几个析构函数之后,没有什么可以继续进行,并且该过程只是一直等待。 它不会做任何事情,不会消耗任何资源。 让进程退出的唯一方法是杀死它,例如Ctrl+C

这不会发生在我的所有脚本中,但我有一个可以复制的例子。 我不确定它是否在我的代码,库代码(主要是Symfony,Doctrince等)或PHP本身。

我已经使用strace运行脚本,并在下面附上了输出的结尾。 我不知道如何调试这个输出,但似乎PHP正在轮询上一条语句中的某些内容。

我怎样才能进一步调试呢? 任何帮助,将不胜感激。

strace输出:

fstat(12, {st_mode=S_IFREG|0777, st_size=1314, ...}) = 0 fstat(12, {st_mode=S_IFREG|0777, st_size=1314, ...}) = 0 fstat(12, {st_mode=S_IFREG|0777, st_size=1314, ...}) = 0 fstat(12, {st_mode=S_IFREG|0777, st_size=1314, ...}) = 0 mmap(NULL, 1314, PROT_READ, MAP_SHARED, 12, 0) = 0x7fb8b969c000 munmap(0x7fb8b969c000, 1314) = 0 close(12) = 0 umask(022) = 022 close(3) = 0 close(4) = 0 write(11, "\1\0\0\0\1", 5) = 5 shutdown(11, SHUT_RDWR) = 0 close(11) = 0 write(6, "\1\0\0\0\0\0\0\0", 8) = 8 close(2) = 0 close(1) = 0 munmap(0x7fb8b969a000, 4096) = 0 close(0) = 0 munmap(0x7fb8b969b000, 4096) = 0 munmap(0x7fb8b3191000, 790528) = 0 munmap(0x7fb8b32d3000, 266240) = 0 munmap(0x7fb8b3314000, 266240) = 0 munmap(0x7fb8b3355000, 266240) = 0 munmap(0x7fb8b3396000, 266240) = 0 munmap(0x7fb8b33d7000, 266240) = 0 munmap(0x7fb8b3418000, 266240) = 0 munmap(0x7fb8b3459000, 266240) = 0 munmap(0x7fb8b349a000, 266240) = 0 munmap(0x7fb8b34db000, 266240) = 0 munmap(0x7fb8b351c000, 266240) = 0 munmap(0x7fb8b94ba000, 266240) = 0 write(10, "\1\0\0\0\0\0\0\0", 8) = 8 poll([{fd=5, events=POLLIN}], 1, 4294967295Process 1358 detached <detached ...>

I have a PHP script that I run on the command line, and when the script is supposed to exit, it just hangs forever instead. It happens on both Windows and Linux so it is not OS related.

I have tried debugging the code with XDebug, stepping into the final exit statement (have tried die as well with no luck). After running through a few destructors on some objects, there is nothing more to step through and the process just waits forever. It does not do anything, not consuming any resources. The only way to get the process to exit is to kill it, e.g Ctrl+C

It does not happen in all my scripts, but I have a case that I can reproduce. I am not sure if it is in my code, library code (mostly Symfony, Doctrince etc.) or in PHP itself.

I have run the script using strace, and the end of the output is attached below. I do not know how to debug this output, but it seems like PHP is polling for something in the last statement.

How can I debug this further? Any help would be appreciated.

strace output:

fstat(12, {st_mode=S_IFREG|0777, st_size=1314, ...}) = 0 fstat(12, {st_mode=S_IFREG|0777, st_size=1314, ...}) = 0 fstat(12, {st_mode=S_IFREG|0777, st_size=1314, ...}) = 0 fstat(12, {st_mode=S_IFREG|0777, st_size=1314, ...}) = 0 mmap(NULL, 1314, PROT_READ, MAP_SHARED, 12, 0) = 0x7fb8b969c000 munmap(0x7fb8b969c000, 1314) = 0 close(12) = 0 umask(022) = 022 close(3) = 0 close(4) = 0 write(11, "\1\0\0\0\1", 5) = 5 shutdown(11, SHUT_RDWR) = 0 close(11) = 0 write(6, "\1\0\0\0\0\0\0\0", 8) = 8 close(2) = 0 close(1) = 0 munmap(0x7fb8b969a000, 4096) = 0 close(0) = 0 munmap(0x7fb8b969b000, 4096) = 0 munmap(0x7fb8b3191000, 790528) = 0 munmap(0x7fb8b32d3000, 266240) = 0 munmap(0x7fb8b3314000, 266240) = 0 munmap(0x7fb8b3355000, 266240) = 0 munmap(0x7fb8b3396000, 266240) = 0 munmap(0x7fb8b33d7000, 266240) = 0 munmap(0x7fb8b3418000, 266240) = 0 munmap(0x7fb8b3459000, 266240) = 0 munmap(0x7fb8b349a000, 266240) = 0 munmap(0x7fb8b34db000, 266240) = 0 munmap(0x7fb8b351c000, 266240) = 0 munmap(0x7fb8b94ba000, 266240) = 0 write(10, "\1\0\0\0\0\0\0\0", 8) = 8 poll([{fd=5, events=POLLIN}], 1, 4294967295Process 1358 detached <detached ...>

最满意答案

我发现这个问题与ZMQ扩展有关。 我的脚本试图向不存在的主机发送消息,即使在我的脚本末尾断开套接字时,套接字也在等待尝试发送数据。

对于任何有相同问题的人,可以通过将套接字上的\ZMQ::SOCKOPT_LINGER选项设置为较低值来解决,例如

<?php $context = new \ZMQContext(1); $socket = new \ZMQSocket($context, \ZMQ::SOCKET_PUSH); $dsn = 'tcp://127.0.0.1:1337'; $socket->connect($dsn); $socket->send('hi'); echo 'Message sent' . PHP_EOL; // Without this line, the script will wait forever after the exit statement $socket->setSockOpt(\ZMQ::SOCKOPT_LINGER, 1000); $socket->disconnect($dsn); echo 'Socket disconnected' . PHP_EOL; exit();

I found that the issue was related to the ZMQ extension. My script was trying to send a message to a non-existing host, and even when disconnecting the socket at the end of my script, the socket was waiting forever trying to send the data.

For anyone having the same problem, it can be resolved by setting the \ZMQ::SOCKOPT_LINGER option on the socket to a low value, e.g.

<?php $context = new \ZMQContext(1); $socket = new \ZMQSocket($context, \ZMQ::SOCKET_PUSH); $dsn = 'tcp://127.0.0.1:1337'; $socket->connect($dsn); $socket->send('hi'); echo 'Message sent' . PHP_EOL; // Without this line, the script will wait forever after the exit statement $socket->setSockOpt(\ZMQ::SOCKOPT_LINGER, 1000); $socket->disconnect($dsn); echo 'Socket disconnected' . PHP_EOL; exit();

更多推荐

本文发布于:2023-07-24 00:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:挂起   进程   CLI   PHP   hangs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!