-
Notifications
You must be signed in to change notification settings - Fork 15
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
base: main
Are you sure you want to change the base?
Conversation
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; | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
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) |
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