php - 如何避免TCP连接卡住PHP Socket(FIN_WAIT1)

标签 php linux symfony tcp debian

我有一个symfony命令,它在Debian9计算机上与主管一起启动时运行。它打开一个TCP侦听器套接字,从端口上的许多设备接收消息。
工作一段时间后(15-20小时),它停止工作并阻塞TCP端口。
这个问题发生在运行php 7.3和apache2的debian 9计算机上。
这是打开TCP套接字的代码:

protected function execute(InputInterface $input, OutputInterface $output)
{
    try {
        $this->io = new SymfonyStyle($input, $output);
        $this->logger->info('Start TCP socket: Setup websocket server for detections on port 8086...');

        $now = new \DateTime();
        $this->io->title('Start server ' . $now->format('d-m-Y G:i:s') . '...');

        $server = socket_create_listen(8086);
        socket_getsockname($server, $addr, $port);
        if (!$server) {
            $message = 'Start TCP socket: Ko. Could not create socket.';
            $this->io->error($message);
            $this->logger->critical($message);
            die($message);
        } else {
            $message = 'Start TCP socket: Ok. TCP socket opened on: ' . $addr . ':' . $port . '.';
            $this->io->success($message);
            $this->logger->info($message);

            while ($c = socket_accept($server)) {
                socket_getpeername($c, $raddr, $rport);
                $this->io->writeln("Received Connection from $raddr:$rport\n");

                $data = '';
                while ($bytes = socket_recv($c, $r_data, 128, MSG_WAITALL)) {
                    $data .= $r_data;
                }

                //Process data here

                socket_close($c);

                $message = 'Finish processing data. Total Data Received: ' . strlen($data) . PHP_EOL;
                $this->io->writeln($message);
                $this->logger->info($message);
            }
        }
        fclose($server);
    } catch (Exception $exception) {
        $message = 'Start TCP socket: Ko. Exception catched. Error detail: ' . $exception->getMessage();
        $this->logger->critical($message);
        $this->io->error($message);
    }
}

当套接字停止连接时,我在控制台中编写以下命令:
sudo netstat -np | grep :8086

它显示此输出:
TCP 0 1 172.25.1.14:8086 88.0.111.77:47794财务等待1-
如何避免此问题并尝试重新启动服务或不阻止端口?
谢谢。

最佳答案

对于特定的问题,tcp终止的正常操作(close())进入FIN_WAIT_1状态,这可能导致打开的套接字保持打开状态,直到它接收到FIN。要解决这个问题,可以通过将SO_LINGER设置为0来告诉套接字不要等待。然而,这被认为是“不好的做法”。有关详细信息,请参阅:TCP option SO_LINGER (zero) - when it's required
如果您使用php作为客户端向该命令发送请求,请用它更新您的问题,因为它可能无法正确终止请求。
似乎您的套接字处理也有一些问题。主要是验证正在使用的资源,并且在发生异常时不关闭套接字。
您应该在finally中添加一个try/catch来处理正常终止或异常,该异常可以关闭打开的套接字。从php 7.0开始,你应该关注的不仅仅是\Throwable。例如,使用\Exceptionintdiv(1, 0)将无法被代码捕获。
假设主管正确监控流程。

protected function execute(InputInterface $input, OutputInterface $output)
{
    try {
        $this->io = new SymfonyStyle($input, $output);
        $this->logger->info('Start TCP socket: Setup websocket server for detections on port 8086...');        

        $now = new \DateTime();
        $this->io->title('Start server ' . $now->format('d-m-Y G:i:s') . '...');

        if (!$localSocket = socket_create_listen(8086)) {
            throw new \RuntimeException('Could not create socket.');
        }
        //force PHP to close the socket (do not linger waiting for FIN)
        socket_set_option($localSocket, SOL_SOCKET, SO_LINGER, [
            'l_linger' => 0,
            'l_onoff' => 1,
        ]);
        if (!socket_getsockname($localSocket, $addr, $port)) {
            throw new \RuntimeException('Unable to retrieve local socket.');
        }

        $message = sprintf('Start TCP socket: Ok. TCP socket opened on: %s:%s.', $addr, $port);
        $this->io->success($message . PHP_EOL);
        $this->logger->info($message);
        $listening = true;
        while ($listening) {
            if (!$remoteSocket = socket_accept($localSocket)) {
                throw new \RuntimeException('Unable to accept incoming connections');
            }
            if (!socket_getpeername($remoteSocket, $raddr, $rport)) {
                throw new \RuntimeException('Unable to retrieve remote socket');
            }
            $this->io->writeln(sprintf('Received Connection from %s:%s%s', $raddr, $rport, PHP_EOL));

            $data = '';
            $bytesRec = 0;
            while ($bytes = socket_recv($remoteSocket, $r_data, 128, MSG_WAITALL)) {
                $data .= $r_data;
                $bytesRec += $bytes;
            }
            //force PHP to close the socket (do not linger waiting for FIN)
            socket_set_option($remoteSocket, SOL_SOCKET, SO_LINGER, [
                'l_linger' => 0, 
                'l_onoff' => 1
            ]);
            //clear memory of remoteSocket resource before processing the data
            socket_close($remoteSocket);
            $remoteSocket  = null;
            unset($remoteSocket);

            //Method Call to process data here...

            $message = sprintf('Finish processing data. Total Data Received: %s %s', $bytesRec, PHP_EOL);
            $this->io->writeln($message);
            $this->logger->info($message);

             if ($condition = false) {
                //add a condition to terminate listening, such as $i++ >= 1000
                $listening = false;
             }

            //force PHP to take a break
            usleep(100);
        }
    } catch (\Throwable $e) {
        $message = sprintf('Start TCP socket: Ko. Exception detail: %s', 
            $e->getMessage());
        $this->logger->critical($message);
        $this->io->error($message);
    } finally {
        //ensure the socket resources are closed
        if (isset($remoteSocket) && is_resource($remoteSocket)) {
            //force PHP to close the socket (do not linger waiting for FIN)
            socket_set_option($remoteSocket, SOL_SOCKET, SO_LINGER, [
                'l_linger' => 0, 
                'l_onoff' => 1
            ]);
            socket_close($remoteSocket);
            $remoteSocket = null;
            unset($remoteSocket);
        }
        if (isset($localSocket) && is_resource($localSocket)) {
            socket_close($localSocket);
            $localSocket = null;
            unset($localSocket);
        }
    }
}

注意:当数据库服务器超时创建的初始连接symfony时,使用docine服务最终将导致数据库连接丢失。这将导致在尝试向数据库发出查询时脚本意外终止。
无论何时调用数据库服务或将其注入命令,都需要关闭连接,在1 << -1期间立即关闭连接。
class YourCommand
{
    private $em;

    public function __construct(EntityManagerInterface $em) 
    {
        $this->em = $em; //or $this->container->get('doctrine.orm.entity_manager')
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->em->getConnection()->close(); 

        //...

        while ($listening) {

            //...

            //Method Call to process data here...
            $this->em->getConnection()->connect();
            //... execute query
            $this->em->getConnection()->close();

            //...
        }
    } 
}

同样值得注意的是,php不打算作为长时间运行的守护进程运行,并且具有execute的特性,例如内存泄漏。建议您找到另一种合适的方法(如nodejs)来代理从远程连接到PHP(如apache和nginx do)的TCP请求。

关于php - 如何避免TCP连接卡住PHP Socket(FIN_WAIT1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56983490/

相关文章:

PHP/MySQL 同步 innoDB 查询作为异步工作

php - Linux 服务器上的 OpenSSL 目录正常 - Windows XAMPP 上不行

symfony - JMS 反序列化未检索对象值

linux -/proc/modules 中模块实例的含义

php - Symfony2 用户提供者必须在登录时返回一个 UserInterface 对象

symfony - 用作 Symfony 参数的非标量 ENV

php - FosUserBundle 在 FilterUserResponseEvent 中重定向

php - 测试数组中的任何结果是否在特定节点中具有值

linux - 需要命令在 perl expect 中并行运行

php - 使用 crontab 执行 PHP 脚本时出错