swoole / swoole-src

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

swoole6.0.1 enable io_uring, when settting 'hook_flags' => SWOOLE_HOOK_ALL, fopen() file failed return false #5693

Closed bingcool closed 1 month ago

bingcool commented 1 month ago

1、php version

/home/wwwroot/swoolefy # php -v
PHP 8.3.15 (cli) (built: Dec 20 2024 20:11:30) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.15, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.15, Copyright (c), by Zend Technologies
/home/wwwroot/swoolefy # 

2、swoole info

/home/wwwroot/swoolefy # php --ri swoole

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 6.0.1
Built => Feb 20 2025 03:58:25
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 3.3.3 11 Feb 2025
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
curl-version => 8.12.1
c-ares => 1.33.1
zlib => 1.3.1
brotli => E16781312/D16781312
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
mysqlnd => enabled
coroutine_pgsql => enabled
coroutine_sqlite => enabled
io_uring => enabled

Directive => Local Value => Master Value
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

3、 test code (设置'hook_flags' => SWOOLE_HOOK_ALL 必现)

<?php
$http = new Swoole\Http\Server('0.0.0.0', 9501);

$http->set([
    'worker_num' => 1,
    'enable_coroutine' => true,
    'hook_flags' => SWOOLE_HOOK_ALL
]);

$http->on('workerStart', function ($serv, $worker_id) {
    $fd = fopen('workerstart.log', 'a+');
    fwrite($fd, "workerStart: {$worker_id}\n");
    var_dump($fd);
});

$http->on('Request', function ($request, $response) {
    $response->header('Content-Type', 'text/html; charset=utf-8');
    $response->end('<h1>Hello Swoole. #' . rand(1000, 9999) . '</h1>');
});

$http->start();

4、fopen error msg

/home/wwwroot/swoolefy # php test.php
[2025-02-22 10:12:43 *4660.0]   WARNING Iouring::Iouring() (ERRNO 502): Create io_uring failed, the error code is 38
PHP Warning:  fopen(workerstart.log): Failed to open stream: No such file or directory in /home/wwwroot/swoolefy/test.php on line 11
PHP Fatal error:  Uncaught TypeError: fwrite(): Argument #1 ($stream) must be of type resource, false given in /home/wwwroot/swoolefy/test.php:12
Stack trace:
#0 /home/wwwroot/swoolefy/test.php(12): fwrite()
#1 [internal function]: {closure}()
#2 {main}
  thrown in /home/wwwroot/swoolefy/test.php on line 12
[2025-02-22 10:12:43 *4660.0]   ERROR   php_swoole_server_rshutdown() (ERRNO 503): Fatal error: Uncaught TypeError: fwrite(): Argument #1 ($stream) must be of type resource, false given in /home/wwwroot/swoolefy/test.php:12
Stack trace:
#0 /home/wwwroot/swoolefy/test.php(12): fwrite()
#1 [internal function]: {closure}()
#2 {main}
  thrown in /home/wwwroot/swoolefy/test.php on line 12
bingcool commented 1 month ago

开启io_uring后,设置hook_flag 包含 SWOOLE_HOOK_FILE 或者 SWOOLE_HOOK_STDIO 都会出现上面报错

NathanFreeman commented 1 month ago

错误码是38 ENOSYS,看一下内核版本信息是不是过低

bingcool commented 1 month ago

是在windows的系统上的基于alpine linux 3.20.3构建的docker里面跑的swoole,如果不设置 SWOOLE_HOOK_FILE 或者 SWOOLE_HOOK_STDIO,fopen()就不会报错。内核版本:

/home/wwwroot/swoolefy # uname -r
5.10.16.3-microsoft-standard-WSL2`
NathanFreeman commented 1 month ago

https://github.com/swoole/swoole-src/issues/5675 看看这个能不能帮你解决问题

NathanFreeman commented 1 month ago

windows的wsl可能没法完全模拟linux环境,导致有些功能不支持

matyhtf commented 1 month ago

参考:

应该是 linux 的 namespace 机制,禁止了 io_uring_queue_init 系统调用,需要修改 docker 的设置:

    security_opt:
      - seccomp:unconfined