automapper icon indicating copy to clipboard operation
automapper copied to clipboard

Fix race condition in EvalLoader

Open MrMeshok opened this issue 1 year ago • 0 comments

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));
    }
});

MrMeshok avatar Oct 11 '24 11:10 MrMeshok