Skip to content

[12.x] Fix Concurrency::run to preserve callback result order #55161

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

Merged
merged 3 commits into from
Mar 27, 2025
Merged
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
2 changes: 2 additions & 0 deletions src/Illuminate/Concurrency/ForkDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function run(Closure|array $tasks): array
/** @phpstan-ignore class.notFound */
$results = Fork::new()->run(...$values);

ksort($results);

return array_combine($keys, $results);
}

Expand Down
37 changes: 37 additions & 0 deletions tests/Integration/Concurrency/ConcurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Process\Factory as ProcessFactory;
use Illuminate\Support\Facades\Concurrency;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;

#[RequiresOperatingSystem('Linux|DAR')]
Expand Down Expand Up @@ -119,6 +120,42 @@ public function testRunHandlerProcessErrorWithCustomExceptionWithParam()
),
]);
}

public static function getConcurrencyDrivers(): array
{
return [
['sync'],
['process'],
// spatie/fork package is not included by default
// ['fork'],
];
}

#[DataProvider('getConcurrencyDrivers')]
public function testRunPreservesCallbackOrder(string $driver)
{
[$first, $second, $third] = Concurrency::driver($driver)->run([
function () {
usleep(1000000);

return 'first';
},
function () {
usleep(500000);

return 'second';
},
function () {
usleep(200000);

return 'third';
},
]);

$this->assertEquals('first', $first);
$this->assertEquals('second', $second);
$this->assertEquals('third', $third);
}
}

class ExceptionWithoutParam extends Exception
Expand Down
Loading