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

Custom Exceptions with Multiple Arguments does not properly reinstantiate with Concurrency::run() #54500

Open
max-shteklia opened this issue Feb 7, 2025 · 2 comments

Comments

@max-shteklia
Copy link

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

  1. 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");
    }
}
  1. 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',
]);
@crynobone

This comment has been minimized.

Copy link

Thank you for reporting this issue!

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.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants