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

Fix race condition in EvalLoader #197

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

MrMeshok
Copy link
Contributor

When using swoole coroutines with eval loader, where is a possibility to get an error Cannot declare class {class}, because the name is already in use, because coroutines share memory and declared classes.
Can be tested like this

require 'vendor/autoload.php';

readonly class Dto
{
    public function __construct(
        public int $id,
    ) {}
}

$mapper = \AutoMapper\AutoMapper::create();
$data = ['id' => 1];

\Co\run(static function () use ($mapper, $data) {
    for ($i=1; $i < 100_000; $i++) { 
        \Co\go(static fn () => $mapper->map($data, Dto::class));
    }
});

Comment on lines +35 to +43
if (isset(self::$lockMap[$mapperMetadata->className])) {
do {
usleep(100000); // 0.1 second
} while (isset(self::$lockMap[$mapperMetadata->className])); // @phpstan-ignore isset.offset

if (class_exists($mapperMetadata->className, false)) {
return;
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code block looks a little weird, but there is a reason for this.
At first, it looked like this

while (isset(self::$lockMap[$mapperMetadata->className])) {
    usleep(100000); // 0.1 second
}

if (class_exists($mapperMetadata->className, false)) {
    return;
}

But there is a problem, class_exists gets called every time, but he only needed if lock existed, so I restructured code to first check lock inside if and then start while loop

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure you could do that if using the LockFactory, anyway an extra call to class_exists should not be a problem

@joelwurtz
Copy link
Member

joelwurtz commented Oct 22, 2024

Indeed, we have done something similar for the FileLoader here #174

Can you use the LockFactory instead of a while loop, like done on this PR ? This way i believe you could use a specific implementation that can handle an event loop for the LockFactory (like one that use this https://www.php.net/manual/fr/class.swoole-lock.php)

Then it will avoid loop + sleep and just block in an async way (allowing you to execute other coroutines)

@MrMeshok MrMeshok marked this pull request as draft October 30, 2024 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants