Skip to content

Switching coroutine contexts during autoload might causes Class "%s" not found error #5770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Appla opened this issue May 15, 2025 · 0 comments

Comments

@Appla
Copy link
Contributor

Appla commented May 15, 2025

问题同#5180 , 是另一种情况

假定有 BasicObject.php

<?php

class BasicObject
{

}

Constant.php

<?php

class Constant extends BasicObject
{
    public const CONST_1 = 'PHP * Swoole',
}

不包含继承关系的类
SimpleConst.php

<?php

class SimpleConst
{
    public const GREETINGS = 'hello world';
}

test.php

<?php

\spl_autoload_register(static function ($clsName) {
    \Swoole\Coroutine::sleep(0.001);
    $path = __DIR__ . '/' . \str_replace('\\', '/', $clsName) . '.php';
    if (\is_file($path)) {
        require $path;
    } else {
        echo "class {$clsName} not found\n";
    }
});

function work_fn(...$args)
{
    echo SimpleConst::GREETINGS . PHP_EOL;
    \Swoole\Coroutine::sleep(\random_int(1, 5) / 1000);
    echo Constant::CONST_1 . PHP_EOL;
}

$closure = static fn() => work_fn();
$scheduler = new \Swoole\Coroutine\Scheduler();
for ($i = 0; $i < 16; ++$i) {
    $scheduler->add($closure, $i);
}
$scheduler->start();

执行可能会包含如下错误

PHP Fatal error:  Uncaught Error: Class "Constant" not found in /path/to/test.php:15
Stack trace:
#0 /path/to/test.php(20): work_fn()
#1 [internal function]: {closure}(1)
#2 {main}
  thrown in /path/to/test.php on line 15

测试从5.1.*~6.0.*都能复现
加载有继承关系的类时, 如有其他coroutine在类未加载完成时同时加载类就会遇到这个问题参考代码

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants