Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Redis-Messenger] Exits with error when Messenger starts consuming messages  #45022

Copy link
Copy link
Closed
@nicoalonso

Description

@nicoalonso
Issue body actions

Symfony version(s) affected

5.4

Description

Container exits with this error when starts consuming messages:

In RedisReceiver.php line 50:
                                                                            
[TypeError]                                                                 
array_key_exists(): Argument #2 ($array) must be of type array, null given

When we debug the code in line 50 of RedisReceiver.php

    /**
     * {@inheritdoc}
     */
    public function get(): iterable
    {
        $message = $this->connection->get();

        if (null === $message) {
            return [];
        }
        dump($message);

        $redisEnvelope = json_decode($message['data']['message'] ?? '', true);

        try {
            if (\array_key_exists('body', $redisEnvelope) && \array_key_exists('headers', $redisEnvelope)) {
                $envelope = $this->serializer->decode([
                    'body' => $redisEnvelope['body'],
                    'headers' => $redisEnvelope['headers'],
                ]);
            } else {
                $envelope = $this->serializer->decode($redisEnvelope);
            }
        } catch (MessageDecodingFailedException $exception) {
            $this->connection->reject($message['id']);

            throw $exception;
        }

        return [$envelope->with(new RedisReceivedStamp($message['id']))];
    }

In message we have:

$message = [
    "id" => "1641827007019-0"
    "data" => null
]

In this case when decode empty string the variable $redisEnvelope is null.

How to reproduce

This error does not always occur, in some cases, this type of message is not received.

In our case, we create a docker image from php:8.0.13-cli-alpine3.13 and install the pecl extension redis-5.3.5

When launch the command like that:

php bin/console messenger:consume async_queue -vv

Possible Solution

    /**
     * {@inheritdoc}
     */
    public function get(): iterable
    {
        $message = $this->connection->get();

        if (null === $message) {
            return [];
        }

        $redisEnvelope = json_decode($message['data']['message'] ?? '', true);

        if (null === $redisEnvelope) {
            return [];
        }

        try {
            if (\array_key_exists('body', $redisEnvelope) && \array_key_exists('headers', $redisEnvelope)) {
                $envelope = $this->serializer->decode([
                    'body' => $redisEnvelope['body'],
                    'headers' => $redisEnvelope['headers'],
                ]);
            } else {
                $envelope = $this->serializer->decode($redisEnvelope);
            }
        } catch (MessageDecodingFailedException $exception) {
            $this->connection->reject($message['id']);

            throw $exception;
        }

        return [$envelope->with(new RedisReceivedStamp($message['id']))];
    }

Additional Context

  • Symfony 5.4
  • PHP-cli 8.0.13
  • ext-redis 5.3.5
  • Redis 6.2.4

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.