Closed
Description
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