Skip to content
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

开两个协程实例化同个类报 Class xxx not found #211

Open
a1312963 opened this issue Aug 7, 2023 · 10 comments
Open

开两个协程实例化同个类报 Class xxx not found #211

a1312963 opened this issue Aug 7, 2023 · 10 comments
Labels
bug Something isn't working

Comments

@a1312963
Copy link

a1312963 commented Aug 7, 2023

image
image

第一个类实例化不报错 正常运行 第二个类报错如图所示

操作系统 windows10 php版本8.1 php框架 Yii 版本2.0.45

@a1312963 a1312963 added the bug Something isn't working label Aug 7, 2023
@twose
Copy link
Member

twose commented Aug 7, 2023

可以贴一个完整的直接可运行的代码么?

@a1312963
Copy link
Author

a1312963 commented Aug 7, 2023

可以贴一个完整的直接可运行的代码么?
image
image
image
image

代码很简单 就这么几行 直接new其他类也是一样错误 不是代码 我看报错的类名字都乱了

image

这样匿名函数外部传参执行没有问题

@a1312963
Copy link
Author

a1312963 commented Aug 8, 2023

image

require包含文件后执行正常 不报错

composer已升级2.5.8 也是同样错误 加到五个协程 第一个协程执行正常 第二个协程报错抛出异常 后面也不执行了

@twose
Copy link
Member

twose commented Aug 8, 2023

我要怎么复现这个 BUG 呢... 有没有最小化的例子?
如果只是你贴出来的代码,过于简单了,类似的多个协程里new一个类的代码比比皆是,看不出引发问题的点在哪里。

@a1312963
Copy link
Author

a1312963 commented Aug 8, 2023

我要怎么复现这个 BUG 呢... 有没有最小化的例子? 如果只是你贴出来的代码,过于简单了,类似的多个协程里new一个类的代码比比皆是,看不出引发问题的点在哪里。

方便加个好友? 我这边把程序发您看看

@a1312963
Copy link
Author

a1312963 commented Aug 8, 2023

image

@a1312963
Copy link
Author

a1312963 commented Aug 9, 2023

最小化验证方式
composer下载yii框架
composer create-project --prefer-dist yiisoft/yii2-app-basic basic

复制下面代码 到commands文件下覆盖HelloController.php

* @SInCE 2.0 */ class HelloController extends Controller { public function actionTest() { //\Yii::$app->cache; $wr = new WaitReference(); for ($i=0; $i<5; $i++){ Coroutine::run(function () use($wr,$i): void { $result = \Yii::$app->cache; echo 'test'; //$instance = new OrdersService(); }); } $wr::wait($wr); } } @twose

@amuluowin
Copy link

大概率是构造函数里面有IO切换造成的

@twose
Copy link
Member

twose commented Nov 7, 2023

参考 swoole/swoole-src#5180

<?php

use Swow\Coroutine;

spl_autoload_register(static function (string $class): void {
    msleep(1); // coroutine context switch
    require_once __DIR__ . '/a.php';
});

for ($i = 0; $i < 3; ++$i) {
    Coroutine::run(static function (): void {
        new A();
        var_dump(Coroutine::getCurrent()->getId());
    });
}

sleep(1);

autoload 里有协程切换会导致这个问题

@twose
Copy link
Member

twose commented Nov 7, 2023

beffeb4

初步修复了,可以完美解决,等我把 CI 修完一起发个版本

@swow swow deleted a comment from AuroraYolo Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants