7

In my Symfony project, there is a queue message handler, and I have an error that randomly appears during the execution:

[2022-10-12T07:31:40.060119+00:00] console.CRITICAL: Error thrown while running command "messenger:consume async --limit=10". Message: "Library error: a socket error occurred" {"exception":"[object] (Symfony\\Component\\Messenger\\Exception
TransportException(code: 0): Library error: a socket error occurred at /var/www/app/vendor/symfony/amqp-messenger/Transport/AmqpReceiver.php:62)
[previous exception] [object] (AMQPException(code: 0): Library error: a socket error occurred at /var/www/app/vendor/symfony/amqp-messenger/Transport/Connection.php:439)","command":"messenger:consume async --limit=10","message":"Library error: a socket error occurred"} []

The handler executes HTTP requests that could last some seconds and the whole process of a single message could even take more than one minute if APIs are slow. The strange thing is that the problem disappears for hours but then it randomly appears again. The more messages are sent to the queue, the easier it's to see the exception.

config\packages\messenger.yaml

framework:
    messenger:    
        transports:
            # https://symfony.com/doc/current/messenger.html#transport-configuration
            async:
                dsn: "%env(MESSENGER_TRANSPORT_DSN)%"
                options:
                    exchange:
                        name: async_exchange
                    queues:
                        async: ~
                    heartbeat: 45
                    write_timeout: 90
                    read_timeout: 90
                retry_strategy:
                    max_retries: 0
            
        routing:
            # Route your messages to the transports
            'App\Message\MessageUpdateRequest': async

App\MessageHandler\MessageUpdateRequestHandler.php

<?php

declare(strict_types=1);

namespace App\MessageHandler;

use App\Message\MessageUpdateRequest;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class MessageUpdateRequestHandler implements MessageHandlerInterface
{

    public function __invoke(MessageUpdateRequest $message)
    {
        // Logic executing API requests...
        return 0;
    }
}

Environment

  • Symfony Messenger: 5.4.17
  • PHP: 8.1
  • RabbitMQ: 3.11.5

Things that I tried

  • upgrading Symfony Messenger to 5.4.17, using the fix available here;
  • adding the following options: heartbeat, write_timeout and read_timeout in the messenger.yaml file.

Related issues/links

How can I fix this issue?

Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
  • did you try to increase the heartbeat like in the lasst comment on issue 258 ?` – john Smith Jan 16 '23 at 20:04
  • Hi @johnSmith. Yes, I tried to increase it but the error is still there. – Davide Pastore Jan 26 '23 at 08:48
  • What is the actual error? Is it on rabbitmq side? on the API side? `(AMQPException(code: 0):` and `Library error: a socket error occurred` is a bit vague. Could you check related error logs on both sides? – Jonathan Jan 29 '23 at 09:42
  • Are you using the latest version of `php-amqplib`? When you see the error message "Library error: a socket error occurred" it *might* be an indication of an outdated version of the library, which might have bugs or compatibility issues with the newer version of RabbitMQ or the Symfony Messenger. Maybe you can try a different transport package; like SimpleBus, to see if the problem is related to the AMQP library or not. – Crimin4L Jan 29 '23 at 17:41
  • @Jonathan, the error is the one included in the question (no additional info is available). The error appears on the client side, and the AMQP server is working in the right way with 0 issues. – Davide Pastore Feb 26 '23 at 21:09
  • @Crimin4L, I'm using the latest version of `php-amqplib` (3.5.1). Using a different transport package (e.g. Redis) solves the problem so this is completely connected to the AMQP transport package. – Davide Pastore Feb 26 '23 at 21:13
  • If solving async mail delivery through systemd instead of rabbitMQ, getting a similar error means the systemd service that runs the `messenger:consume` is not running or has insufficient permissions. Can you see everything is running with `ps aux` ? Maybe this helps. – Jonathan Feb 27 '23 at 07:10
  • @Jonathan, unfortunately, this is happening randomly and not constantly. The permissions are set up correctly because, otherwise, the command would show me an error every time I launch it. – Davide Pastore Feb 27 '23 at 14:28

1 Answers1

2

Regarding a socket error that occurs in Symfony Messenger, I always suggest following the step-wise approach and checking if you are missing anything. It should fix this type of error almost every time. Please follow these guidelines:

  • Verify that the RabbitMQ service is active and accessible.
  • Verify that the hostname, port, username, and password are listed in the messenger.yaml file are accurate.
  • In the messenger.yaml file, increase the heartbeat, write timeout, and read timeout settings.
  • Verify your use case to determine whether the max retries number in messenger.yaml is appropriate.
  • Look for any network problems that could be causing the socket error.
  • Make sure your PHP version is compatible with RabbitMQ and Symfony Messenger.
  • Verify that the server's resources (CPU, Memory, and Disk) are not used up.
  • Look for any relevant error messages in the PHP error log.
  • Determine whether there is a problem with the MessageUpdateRequestHandler class's logic.

Hope it helps. Happy debugging and good luck.

Muhammad Asadullah
  • 3,735
  • 1
  • 22
  • 38