swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.42k stars 3.16k forks source link

Worker freezes after restart when using worker_max_concurrency #5430

Closed allsilaevex closed 2 months ago

allsilaevex commented 2 months ago

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error.

To reproduce the problem you need to run php server.php & php client.php and wait about 5 seconds

server.php

<?php

$server = new Swoole\Http\Server('0.0.0.0', 9999);

$server->set([
    'worker_num' => 2,
    'max_connection' => 128,
    'worker_max_concurrency' => 8, // this is the reason for bug
]);

$server->on('WorkerStart', static function (Swoole\Http\Server $server, int $workerId) {
    if ($workerId == 0) {
        Swoole\Timer::after(2000, static function () use ($server) {
            $server->stop(1);
        });
    }
});

$server->on('Request', static function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
    \Swoole\Coroutine::sleep(seconds: rand(50, 100) / 1000);
    $response->end();
});

$server->start();

client.php

<?php

declare(strict_types=1);

$callback = static function () {
    Swoole\Coroutine\run(static function () {
        while (true) {
            Swoole\Coroutine::sleep(rand(15, 35) / 100.0);

            $request = static function () {
                try {
                    $response = Swoole\Coroutine\Http\get(url: 'http://0.0.0.0:9999/', options: ['timeout' => 5.0]);
                } catch (Throwable $exception) {
                    echo '[error] error: ' . $exception->getMessage() . PHP_EOL;
                    return;
                }

                if ($response->getStatusCode() > 299) {
                    echo '[error] status code = ' . $response->getStatusCode() . PHP_EOL;
                }
            };

            for ($i = 1; $i <= 5; $i++) {
                Swoole\Coroutine\go($request);
            }
        }
    });
};

for ($n = 1; $n <= 8; $n++) {
    $process = new Swoole\Process($callback);
    $process->start();
}

for ($n = 8; $n--;) {
    $status = Swoole\Process::wait();
}
  1. What did you expect to see?

0 errors

  1. What did you see instead?

error messages like:

WARNING Server::accept_connection() (ERRNO 9002): Too many connections [now: 128]
[error] error: Connection reset by peer
[error] error: Operation timed out
  1. What version of Swoole are you using (show your php --ri swoole)?
swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 5.1.2
Built => Jun  4 2024 09:09:32
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 3.3.0 9 Apr 2024
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
pcre => enabled
c-ares => 1.28.1
zlib => 1.3.1
brotli => E16781312/D16781312
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
mysqlnd => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_fiber_mock => Off => Off
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608
  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ?
8be9af41998a:/app$ uname -a
Linux 8be9af41998a 5.15.0-116-generic #126-Ubuntu SMP Mon Jul 1 10:14:24 UTC 2024 x86_64 Linux
8be9af41998a:/app$ php -v
PHP 8.3.7 (cli) (built: May 22 2024 23:46:10) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.7, Copyright (c), by Zend Technologies
8be9af41998a:/app$ gcc -v
bash: gcc: command not found
NathanFreeman commented 2 months ago

Fixed by https://github.com/swoole/swoole-src/pull/5413

allsilaevex commented 2 months ago

@NathanFreeman yes, you are right I tested in nightly image and the problem does not reproduce thanks!