Skip to content

Symfony/messenger : Serialization of 'Closure' is not allowed #45262

Closed
@pesseyjulien

Description

@pesseyjulien

Symfony version(s) affected

5.4.3

Description

Hi,

New to using symfony/messenger, I followed the doc : https://symfony.com/doc/5.4/messenger.html and did a small implementation.

Everything works fine on local but then on my server (heroku) I'm getting the following issue when running the consumer command :

 [OK] Consuming messages from transports "async".


 // The worker will automatically exit once it has received a stop signal via the messenger:stop-workers command.

 // Quit the worker with CONTROL-C.

 // Re-run the command with a -vv option to see logs about consumed messages.

{"message":"Received message AppBundle\\Message\\CurlRequest","context":{"message":{},"class":"AppBundle\\Message\\CurlRequest"},"level":200,"level_name":"INFO","channel":"messenger","datetime":"2022-01-31T18:54:15.008761+00:00","extra":{}}
{"message":"Message AppBundle\\Message\\CurlRequest handled by AppBundle\\MessageHandler\\CurlRequestHandler::__invoke","context":{"message":{},"class":"AppBundle\\Message\\CurlRequest","handler":"AppBundle\\MessageHandler\\CurlRequestHandler::__invoke"},"level":200,"level_name":"INFO","channel":"messenger","datetime":"2022-01-31T18:54:15.293857+00:00","extra":{}}
{"message":"Error thrown while running command \"messenger:consume async\". Message: \"Serialization of 'Closure' is not allowed\"","context":{"exception":{"class":"Exception","message":"Serialization of 'Closure' is not allowed","code":0,"file":"/app/vendor/inspector-apm/inspector-symfony/src/Listeners/MessengerEventsSubscriber.php:81"},"command":"messenger:consume async","message":"Serialization of 'Closure' is not allowed"},"level":500,"level_name":"CRITICAL","channel":"console","datetime":"2022-01-31T18:54:15.439893+00:00","extra":{}}
18:54:15 CRITICAL  [console] Error thrown while running command "messenger:consume async". Message: "Serialization of 'Closure' is not allowed" ["exception" => Exception { …},"command" => "messenger:consume async","message" => "Serialization of 'Closure' is not allowed"]
{"message":"Command \"messenger:consume async\" exited with code \"1\"","context":{"command":"messenger:consume async","code":1},"level":100,"level_name":"DEBUG","channel":"console","datetime":"2022-01-31T18:54:15.458841+00:00","extra":{}}

In MessengerEventsSubscriber.php line 81:

  Serialization of 'Closure' is not allowed

The message is not being deleted from the table but seems to be handle 'correctly'.

Any idea why I'm getting this error ?

My Config :

framework:
    messenger:
        # Uncomment this (and the failed transport below) to send failed messages to this transport for later handling.
        failure_transport: failed
        transports:
            async: '%env(MESSENGER_TRANSPORT_DSN)%'
            failed: '%env(MESSENGER_TRANSPORT_DSN)%?queue_name=failed'
            #sync: 'sync://'
        routing:
            # Route your messages to the transports
            'AppBundle\Message\CurlRequest': async

Handler :

#[AsMessageHandler]
class CurlRequestHandler
{

    public function __construct(private Curl $curl)
    {
    }

    public function __invoke(CurlRequest $message)
    {
        $this->curl->sendCurl($message->getUrl(), $message->getMethod(), $message->getContent(), $message->getApiKey());

    }

}

Message :

class CurlRequest
{

    private string $url;

    private string $method;

    private ?string $apiKey = null;

    private ?string $content = null;


    public function __construct(string $url,
        string $method,
        string $content=null,
        string $apiKey=null)
    {
        $this->url = $url;
        $this->method = $method;
        if($content) $this->content = $content;
        if($apiKey) $this->apiKey = $apiKey;
    }

    public function getContent(): ?string
    {
        return $this->content;
    }

    public function getUrl(): string
    {
        return $this->url;
    }

    public function getMethod(): string
    {
        return $this->method;
    }

    public function getApiKey(): ?string
    {
        return $this->apiKey;
    }

}

My dispatch :

...
if($body) $body = \json_encode($body);
$this->bus->dispatch(new CurlRequest($url, $method, $body));

Thanks in advance for your help,
Julien

How to reproduce

Consume a message stored like :

8,"O:36:\""Symfony\\Component\\Messenger\\Envelope\"":2:{s:44:\""\0Symfony\\Component\\Messenger\\Envelope\0stamps\"";a:1:{s:46:\""Symfony\\Component\\Messenger\\Stamp\\BusNameStamp\"";a:1:{i:0;O:46:\""Symfony\\Component\\Messenger\\Stamp\\BusNameStamp\"":1:{s:55:\""\0Symfony\\Component\\Messenger\\Stamp\\BusNameStamp\0busName\"";s:21:\""messenger.bus.default\"";}}}s:45:\""\0Symfony\\Component\\Messenger\\Envelope\0message\"";O:29:\""AppBundle\\Message\\CurlRequest\"":4:{s:34:\""\0AppBundle\\Message\\CurlRequest\0url\"";s:105:\""https://api.hubapi.com/contacts/v1/contact/vid/22351/profile?hapikey=add3feb9-532f-41c4-b4f2-d618155e558d\"";s:37:\""\0AppBundle\\Message\\CurlRequest\0method\"";s:4:\""POST\"";s:37:\""\0AppBundle\\Message\\CurlRequest\0apiKey\"";N;s:38:\""\0AppBundle\\Message\\CurlRequest\0content\"";s:857:\""{\""properties\"":[{\""property\"":\""username\"",\""value\"":\""emiliema1\""},{\""property\"":\""firstname\"",\""value\"":\""Emilie22\""},{\""property\"":\""lastname\"",\""value\"":\""Masselis8\""},{\""property\"":\""phone\"",\""value\"":\""0640228374\""},{\""property\"":\""email\"",\""value\"":\""emilie+002@toquesderestauration.com\""},{\""property\"":\""jobtitle\"",\""value\"":\""G\\u00e9rant\""},{\""property\"":\""hs_language\"",\""value\"":\""fr\""},{\""property\"":\""activated_free_trial\"",\""value\"":false},{\""property\"":\""total_applications\"",\""value\"":0},{\""property\"":\""is_verified\"",\""value\"":false},{\""property\"":\""is_registered\"",\""value\"":true},{\""property\"":\""depts\"",\""value\"":\""73\""},{\""property\"":\""sql_level\"",\""value\"":\""\""},{\""property\"":\""sql_level_minus\"",\""value\"":\""app;\""},{\""property\"":\""id_myworks\"",\""value\"":\""a850437b-2f1e-4ea9-8795-ef7fe5af4451\""},{\""property\"":\""hs_content_membership_status\"",\""value\"":\""active\""},{\""property\"":\""profile\"",\""value\"":\""pro\""},{\""property\"":\""created_at\"",\""value\"":\""1642464000000\""}]}\"";}}",[],default,2022-01-31 18:31:43,2022-01-31 18:31:43,2022-01-31 18:32:06

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions