You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Laravel's Concurrency::run() does not properly handle exceptions that require multiple constructor arguments. When an exception is thrown inside a concurrent task, Laravel only serializes the message property, and when it reinstantiates the exception in the parent process, it calls the constructor with only $message instead of all expected arguments.
This causes an ArgumentCountError when the exception constructor requires multiple parameters.
I used debugger to investigate what happens
In my code example below I was able to find that throw new ApiRequestException() happens once, but parent::construct called twice, first time using proper passed args, but the second time it goes only with one argument which is "API request to {$uri} failed with status $statusCode $reason" in my case. I have tried to put args to parent::__construct like ("message", $code, ...) but it wouldn't work for me (I assume that it is because it calls construct of Exception class). ChatGPT explained that issue like that (I'm not saying that it is correct thing and also it was unable to give me any workaround of that):
This problem occurs because Laravel’s Concurrency::run() spawns subprocesses for concurrent tasks. When an exception is thrown inside a subprocess, Laravel serializes it to pass it back to the main process. However, Laravel only retains the message property when serializing, and when it attempts to reinstantiate the exception, it calls the constructor with only $message, instead of all required arguments, causing an ArgumentCountError.
Steps To Reproduce
Create a Custom Exception with Multiple Arguments
<?php
namespace App\Exceptions;
use Exception;
class ApiRequestException extends Exception
{
public function __construct(
public string $uri,
public int $statusCode,
public string $reason,
public string|array $responseBody = '',
) {
parent::__construct("API request to {$uri} failed with status $statusCode $reason");
}
}
Throw the Exception Inside a Concurrent Task
<?php
use Illuminate\Support\Facades\Concurrency;
use App\Exceptions\ApiRequestException;
[$result1, $result2] = Concurrency::run([
fn () => throw new ApiRequestException('https://api.example.com', 400, 'Bad Request', 'Invalid payload'),
fn () => 'Task 2 completed',
]);
The text was updated successfully, but these errors were encountered:
As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.
If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.
Laravel Version
11.41.3
PHP Version
8.2.27
Database Driver & Version
No response
Description
Laravel's Concurrency::run() does not properly handle exceptions that require multiple constructor arguments. When an exception is thrown inside a concurrent task, Laravel only serializes the message property, and when it reinstantiates the exception in the parent process, it calls the constructor with only $message instead of all expected arguments.
This causes an ArgumentCountError when the exception constructor requires multiple parameters.
I used debugger to investigate what happens
In my code example below I was able to find that throw new ApiRequestException() happens once, but parent::construct called twice, first time using proper passed args, but the second time it goes only with one argument which is
"API request to {$uri} failed with status $statusCode $reason"
in my case. I have tried to put args to parent::__construct like ("message", $code, ...) but it wouldn't work for me (I assume that it is because it calls construct of Exception class). ChatGPT explained that issue like that (I'm not saying that it is correct thing and also it was unable to give me any workaround of that):This problem occurs because Laravel’s Concurrency::run() spawns subprocesses for concurrent tasks. When an exception is thrown inside a subprocess, Laravel serializes it to pass it back to the main process. However, Laravel only retains the message property when serializing, and when it attempts to reinstantiate the exception, it calls the constructor with only $message, instead of all required arguments, causing an ArgumentCountError.
Steps To Reproduce
The text was updated successfully, but these errors were encountered: