workerman重连捕获异常是无效的,依然会导致进程重启

Jack997

有两个连接分别处理不同业务
连接A WebSocket协议连接,属于内网连接(处理主要业务)
连接B Mqtt协议连接,属于外网连接(协助处理云端业务,两者是在同一个进程内工作)
这时候外网中断,连接B断开连接,执行重连机制,重连失败报错而导致进程重启(影响连接A的业务),如何保证连接B重连不影响进程中连接A运行

目前使用
try{

} catch (\Throwable $e) {

}
捕获异常是无效的,依然会导致重启

有什么好的方案可以解决此类问题呢?
目前用版本
"php": "7.3.3",
"workerman/workerman": "4.1.15",
"workerman/mqtt": "1.6",

118 1 1
1个回答

walkor 打赏

应该是你try catch放错位置了,发下具体报错包括调用栈,还有重现问题的代码

  • Jack997 18小时前

    代码片段
    $mqtt->onClose = function (Client $mqtt) {
    try {
    $mqtt->reConnect();
    } catch (\Throwable $e) {

    }

    }

    try {
    $mqtt->connect()
    } catch (\Throwable $e) {

    }

    错误
    pid:26848 think\exception\ErrorException: stream_socket_client(): php_network_getaddresses: getaddrinfo failed: Name or service not known

  • walkor 18小时前

    调用栈没贴

  • Jack997 18小时前

    connect 与 reConnect 我都有try catch 包起来,不知具体是要包在哪个位置呢?

  • walkor 18小时前

    你要贴下调用栈,好判断异常从哪里出来的

  • Jack997 18小时前

    2024-11-22 11:21:42 pid:26845 Workerman[think] start in DAEMON mode
    2024-11-22 11:23:43 pid:26848 think\exception\ErrorException: stream_socket_client(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/php_server/gate_control_v3/vendor/workerman/workerman/Connection/AsyncTcpConnection.php:197
    Stack trace:

    0 [internal function]: think\initializer\Error->appError(2, 'stream_socket_c...', '/home/php_serve...', 197, Array)

    1 /home/php_server/gate_control_v3/vendor/workerman/workerman/Connection/AsyncTcpConnection.php(197): stream_socket_client('tcp://mqtt.yimi...', 0, 'php_network_get...', 0, 2)

    2 /home/php_server/gate_control_v3/vendor/workerman/workerman/Events/Select.php(238): Workerman\Connection\AsyncTcpConnection->connect()

    3 /home/php_server/gate_control_v3/vendor/workerman/workerman/Events/Select.php(299): Workerman\Events\Select->tick()

    4 /home/php_server/gate_control_v3/vendor/workerman/workerman/Worker.php(1638): Workerman\Events\Select->loop()

    5 /home/php_server/gate_control_v3/vendor/workerman/workerman/Worker.php(1429): Workerman\Worker::forkOneWorkerForLinux(Object(Workerman\Worker))

    6 /home/php_server/gate_control_v3/vendor/workerman/workerman/Worker.php(1403): Workerman\Worker::forkWorkersForLinux()

    7 /home/php_server/gate_control_v3/vendor/workerman/workerman/Worker.php(560): Workerman\Worker::forkWorkers()

    8 /home/php_server/gate_control_v3/app/server/GcServerEvent.php(161): Workerman\Worker::runAll()

    9 /home/php_server/gate_control_v3/app/command/GcServer.php(153): app\server\GcServerEvent::run()

    10 /home/php_server/gate_control_v3/vendor/topthink/framework/src/think/console/Command.php(210): app\command\GcServer->execute(Object(think\console\Input), Object(think\console\Output))

    11 /home/php_server/gate_control_v3/vendor/topthink/framework/src/think/Console.php(654): think\console\Command->run(Object(think\console\Input), Object(think\console\Output))

    12 /home/php_server/gate_control_v3/vendor/topthink/framework/src/think/Console.php(313): think\Console->doRunCommand(Object(app\command\GcServer), Object(think\console\Input), Object(think\console\Output))

    13 /home/php_server/gate_control_v3/vendor/topthink/framework/src/think/Console.php(250): think\Console->doRun(Object(think\console\Input), Object(think\console\Output))

    14 /home/php_server/gate_control_v3/think(8): think\Console->run()

    15 {main}

    2024-11-22 11:23:43 pid:26849 think\exception\ErrorException: stream_socket_client(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/php_server/gate_control_v3/vendor/workerman/workerman/Connection/AsyncTcpConnection.php:197
    Stack trace:

    0 [internal function]: think\initializer\Error->appError(2, 'stream_socket_c...', '/home/php_serve...', 197, Array)

    1 /home/php_server/gate_control_v3/vendor/workerman/workerman/Connection/AsyncTcpConnection.php(197): stream_socket_client('tcp://mqtt.yimi...', 0, 'php_network_get...', 0, 2)

    2 /home/php_server/gate_control_v3/vendor/workerman/workerman/Events/Select.php(238): Workerman\Connection\AsyncTcpConnection->connect()

    3 /home/php_server/gate_control_v3/vendor/workerman/workerman/Events/Select.php(299): Workerman\Events\Select->tick()

    4 /home/php_server/gate_control_v3/vendor/workerman/workerman/Worker.php(1638): Workerman\Events\Select->loop()

    5 /home/php_server/gate_control_v3/vendor/workerman/workerman/Worker.php(1429): Workerman\Worker::forkOneWorkerForLinux(Object(Workerman\Worker))

    6 /home/php_server/gate_control_v3/vendor/workerman/workerman/Worker.php(1403): Workerman\Worker::forkWorkersForLinux()

    7 /home/php_server/gate_control_v3/vendor/workerman/workerman/Worker.php(560): Workerman\Worker::forkWorkers()

    8 /home/php_server/gate_control_v3/app/server/GcServerEvent.php(161): Workerman\Worker::runAll()

    9 /home/php_server/gate_control_v3/app/command/GcServer.php(153): app\server\GcServerEvent::run()

    10 /home/php_server/gate_control_v3/vendor/topthink/framework/src/think/console/Command.php(210): app\command\GcServer->execute(Object(think\console\Input), Object(think\console\Output))

    11 /home/php_server/gate_control_v3/vendor/topthink/framework/src/think/Console.php(654): think\console\Command->run(Object(think\console\Input), Object(think\console\Output))

    12 /home/php_server/gate_control_v3/vendor/topthink/framework/src/think/Console.php(313): think\Console->doRunCommand(Object(app\command\GcServer), Object(think\console\Input), Object(think\console\Output))

    13 /home/php_server/gate_control_v3/vendor/topthink/framework/src/think/Console.php(250): think\Console->doRun(Object(think\console\Input), Object(think\console\Output))

    14 /home/php_server/gate_control_v3/think(8): think\Console->run()

    15 {main}

    2024-11-22 11:23:43 pid:26847 worker[GcServer-2070-201:26849] exit with status 64000
    2024-11-22 11:23:43 pid:26847 worker[GcServer-2070-202:26848] exit with status 64000

  • Jack997 17小时前

    我这里是基于thinkphp6 Command 来运行Workerman 是不是需要再think 里面去捕获这个错误

  • walkor 17小时前

    https://github.com/walkor/workerman/blob/4.2/Connection/AsyncTcpConnection.php
    复制这个文件覆盖你本地的,然后重启试下

  • Jack997 17小时前

    可以了 加的这行生效了 restore_error_handler()

  • Jack997 17小时前

    这是是不是意味着 在其他地方设置的 set_error_handler() 导致调用位置发生了改变

  • walkor 17小时前

    应该tp是设置了 set_error_handler 并抛出了异常的

×
🔝