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
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [GH#185](https://github.com/jolicode/automapper/pull/185) Fix constructor with default parameter array does not work with constructor_arguments context
- [GH#187](https://github.com/jolicode/automapper/pull/187) Fix regression after [GH#184](https://github.com/jolicode/automapper/pull/184)
- [GH#192](https://github.com/jolicode/automapper/pull/192) Fix source and context not passed to callable transformer
- [GH#197](https://github.com/jolicode/automapper/pull/197) Fix race condition in EvalLoader

## [9.1.2] - 2024-09-03
### Fixed
Expand Down
34 changes: 25 additions & 9 deletions src/Loader/EvalLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,40 @@
*
* @author Joel Wurtz <[email protected]>
*
* @internal
* @internal
*/
final readonly class EvalLoader implements ClassLoaderInterface
final class EvalLoader implements ClassLoaderInterface
{
private PrettyPrinterAbstract $printer;
/** @var array<class-string, true> */
private static array $lockMap = [];

public function __construct(
private MapperGenerator $generator,
private MetadataFactory $metadataFactory,
private readonly MapperGenerator $generator,
private readonly MetadataFactory $metadataFactory,
private readonly PrettyPrinterAbstract $printer = new Standard(),
) {
$this->printer = new Standard();
}

public function loadClass(MapperMetadata $mapperMetadata): void
{
eval($this->printer->prettyPrint($this->generator->generate(
$this->metadataFactory->getGeneratorMetadata($mapperMetadata->source, $mapperMetadata->target)
)));
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;
}
}
Comment on lines +35 to +43
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


self::$lockMap[$mapperMetadata->className] = true;
try {
eval($this->printer->prettyPrint($this->generator->generate(
$this->metadataFactory->getGeneratorMetadata($mapperMetadata->source, $mapperMetadata->target)
)));
} finally {
unset(self::$lockMap[$mapperMetadata->className]);
}
}

public function buildMappers(MetadataRegistry $registry): bool
Expand Down
Loading