如何避免TCP连接卡住PHP套接字(FIN_WAIT1)

时间:2019-07-11 07:09:35

标签: php linux symfony tcp debian

我有一个Symfony命令,该命令在debian 9计算机上使用超级用户启动时运行。它会打开一个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 FIN_WAIT1-

如何避免此问题并尝试重新启动服务或不阻止端口?

谢谢。

1 个答案:

答案 0 :(得分:0)

对于特定的问题,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而不只是\Exception。例如,使用intdiv(1, 0)1 << -1可能会被您的代码捕获。

假设主管正确地监视了过程。

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创建的初始连接超时时,使用Doctrine服务最终将导致数据库连接丢失。当您尝试向数据库发出查询时,这将导致脚本意外终止。

无论何时调用数据库服务或将其注入到命令中,您都需要关闭连接。在execute期间立即关闭连接。

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并非旨在作为长时间运行的守护进程运行,并且具有known issues with doing so,例如内存泄漏。强烈建议您找到另一种合适的方法,例如NodeJS,来代理从您的远程连接到PHP的TCP请求(如Apache和NGINX那样)。