启用crontab后运行程序报错 ERROR php_swoole_server_rshutdown (ERRNO 503)

Open
#6 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
backend

Research direction

Start by reproducing the failure with config/autoload/crontab.php enabled and the task_worker settings and callbacks shown in config/autoload/server.php. Run the LogCleanerTask example and inspect the stack through vendor/symfony/console/Output/StreamOutput.php:76 and Hyperf\Framework\Logger\StdoutLogger. Done means the scheduled task can log without the socket coroutine fatal error or abnormal worker exit.

Written by the indexing model from the issue text.

Description

环境
PHP 7.3.26
swoole 4.6.1
hyperf v2.1.1

配置config\autoload\crontab.php

return [
    // 是否开启定时任务
    'enable' => true,
];

config\autoload\server.php

'settings' => [
        ...
        // Task Worker 数量,根据您的服务器配置而配置适当的数量
        'task_worker_num' => 1,
        // 因为 `Task` 主要处理无法协程化的方法,所以这里推荐设为 `false`,避免协程下出现数据混淆的情况
        'task_enable_coroutine' => false,
    ],
    'callbacks' => [
        ...
        // Task callbacks
        Event::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
        Event::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
    ],

运行官方文档示例

<?php

declare(strict_types=1);
namespace App\Task;

use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;

/**
 * @Crontab(name="LogCleanerTask", rule="*\/5 * * * * *", callback="execute")
 */
class LogCleanerTask
{
    /**
     * @Inject()
     * @var \Hyperf\Contract\StdoutLoggerInterface
     */
    private $logger;

    public function execute()
    {
        $this->logger->info(date('Y-m-d H:i:s', time()));
    }
}

报错

[2021-02-06 16:51:00 *1514.0]   ERROR   php_swoole_server_rshutdown (ERRNO 503): Fatal error: Uncaught Swoole\Error: Socket#35 has already been bound to another coroutine#20, writing of the same socket in coroutine#21 at the same time is not allowed in /data/wwwroot/hyperf-api/vendor/symfony/console/Output/StreamOutput.php:76
Stack trace:
#0 /data/wwwroot/hyperf-api/vendor/symfony/console/Output/StreamOutput.php(76): fwrite(Resource id #1632, '\e[32m[DEBUG]\e[3...')
#1 /data/wwwroot/hyperf-api/vendor/symfony/console/Output/Output.php(166): Symfony\Component\Console\Output\StreamOutput->doWrite('\e[32m[DEBUG]\e[3...', true)
#2 /data/wwwroot/hyperf-api/vendor/symfony/console/Output/Output.php(132): Symfony\Component\Console\Output\Output->write(Array, true, 1)
#3 /data/wwwroot/hyperf-api/vendor/hyperf/framework/src/Logger/StdoutLogger.php(138): Symfony\Component\Console\Output\Output->writeln('<info>[DEBUG]</...')
#4 /data/wwwroot/hyperf-api/vendor/hyperf/framework/src/Logger/StdoutLogger.php(113): Hyperf\Framework\Logger\StdoutLogger->log('debug', '<info>[DEBUG]</...', Array)
#5 /data/wwwroot/hyperf-ap in /data/wwwroot/hyperf-api/vendor/symfony/console/Output/StreamOutput.php on line 76
[2021-02-06 16:51:00 $1510.0]   WARNING check_worker_exit_status: worker#0[pid=1514] abnormal exit, status=255, signal=0

关闭crontab则不会报这个错误

Dominant language
PHP
Stars
13
Forks
13
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from hyperf/crontab

All issues in hyperf/crontab

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.