swooletw / laravel-swoole

High performance HTTP server based on Swoole. Speed up your Laravel or Lumen applications.
MIT License
4.04k stars 390 forks source link

Fix: ref different coroutine id on same request #488

Closed m3m0r7 closed 3 years ago

m3m0r7 commented 3 years ago

In same request, Context::getCoroutineId will return diffirent value when using it on different coroutines. This problem is easy to make a mistake when using coroutines with laravel-swoole.

For example is below:

// ...

$request1 = $sandbox->getRequest();

go(function () use ($sandbox) {
    // $request1 and $request2 are different reference because coroutine ids are not same.
    $request2 = $sandbox->getRequest();
});

// ...

and another way:

// ...

$value1 = Context::getData('foo');

go(function () use ($sandbox) {
    // $value1 and $value2 are different reference because coroutine ids are not same.
    $value2 = Context::getData('foo');
});

// ...

This PR is fixed it.

albertcht commented 3 years ago

Hi @m3m0r7 ,

Thanks for your pull request! I will merge this PR soon.

m3m0r7 commented 3 years ago

Thx!