Skip to content

[Cache] "Trying to access array offset on value of type bool" warning with RedisAdapter #46384

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

Closed
mbabker opened this issue May 17, 2022 · 3 comments

Comments

@mbabker
Copy link
Contributor

mbabker commented May 17, 2022

Symfony version(s) affected

5.4.8

Description

Quite routinely in our Sentry logs, we're getting the noted warning being captured when trying to write the result of a query to Redis for caching. I'm only including the part of the stack trace from our repository's call to Doctrine\ORM\AbstractQuery::getResult() as the rest isn't really relevant:

ErrorException: Warning: Trying to access array offset on value of type bool
#30 /vendor/symfony/cache/Traits/RedisTrait.php(562): Symfony\Component\Cache\Adapter\RedisAdapter::pipeline
#29 /vendor/symfony/cache/Traits/RedisTrait.php(499): Symfony\Component\Cache\Adapter\RedisAdapter::doSave
#28 /vendor/symfony/cache/Adapter/AbstractAdapter.php(167): Symfony\Component\Cache\Adapter\AbstractAdapter::commit
#27 /vendor/symfony/cache/Traits/AbstractAdapterTrait.php(276): Symfony\Component\Cache\Adapter\AbstractAdapter::save
#26 /vendor/doctrine/dbal/src/Connection.php(1108): Doctrine\DBAL\Connection::executeCacheQuery
#25 /vendor/doctrine/dbal/src/Connection.php(1026): Doctrine\DBAL\Connection::executeQuery
#24 /vendor/doctrine/orm/lib/Doctrine/ORM/Query/Exec/SingleSelectExecutor.php(31): Doctrine\ORM\Query\Exec\SingleSelectExecutor::execute
#23 /vendor/doctrine/orm/lib/Doctrine/ORM/Query.php(325): Doctrine\ORM\Query::_doExecute
#22 /vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php(1188): Doctrine\ORM\AbstractQuery::executeIgnoreQueryCache
#21 /vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php(1142): Doctrine\ORM\AbstractQuery::execute
#20 /vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php(878): Doctrine\ORM\AbstractQuery::getResult

ORM 2.12.2 and DBAL 3.3.6 are in use here, with ext/redis at 5.3.6 and Redis 6.x servers.

How to reproduce

I'm not able to recreate it in any environment but our production system, but the calling code in our app isn't anything crazy repository class:

return $this->createQueryBuilder('alert')
    ->leftJoin('alert.attachments', 'attachment')
    ->where('alert.published_from <= :now')
    ->andWhere('alert.published_to >= :now')
    ->orderBy('alert.published_from')
    ->addOrderBy('alert.title')
    ->setParameter('now', new \DateTimeImmutable('now', new \DateTimeZone('UTC')))
    ->getQuery()
    ->enableResultCache(600, 'active_alerts')
    ->getResult();

Possible Solution

No response

Additional Context

No response

@nicolas-grekas
Copy link
Member

RedisTrait.php(562)

I can't find any array access on this line. Looks like my lines aren't yours. What's this line like for you?

@mbabker
Copy link
Contributor Author

mbabker commented May 23, 2022

@joaopbnogueira
Copy link

joaopbnogueira commented Oct 24, 2022

FYI, same issue here (on symfony 5.4 but it looks the code is exactly the same even on 6.x)

PHP Notice: Trying to access array offset on value of type bool in /var/www/vendor/symfony/cache/Traits/RedisTrait.php

When using phpredis the result of https://github.com/symfony/cache/blob/v5.4.8/Traits/RedisTrait.php#L553 is always "true". Not sure what happens with other redis drivers.

A potential solution would be something along these lines:

foreach ($ids as $k => $id) {
            yield $id => is_array($results) ? $results[$k] : $results;
        }

joaopbnogueira pushed a commit to joaopbnogueira/symfony-cache that referenced this issue Oct 24, 2022
joaopbnogueira pushed a commit to joaopbnogueira/symfony-cache that referenced this issue Oct 24, 2022
joaopbnogueira pushed a commit to joaopbnogueira/symfony-cache that referenced this issue Oct 24, 2022
joaopbnogueira pushed a commit to joaopbnogueira/symfony-cache that referenced this issue Oct 24, 2022
joaopbnogueira pushed a commit to joaopbnogueira/symfony-cache that referenced this issue Oct 24, 2022
joaopbnogueira pushed a commit to joaopbnogueira/symfony-cache that referenced this issue Oct 24, 2022
nicolas-grekas pushed a commit to joaopbnogueira/symfony-cache that referenced this issue Dec 13, 2022
nicolas-grekas added a commit that referenced this issue Dec 13, 2022
… bool (João Nogueira)

This PR was merged into the 5.4 branch.

Discussion
----------

[Cache] Fix dealing with ext-redis' multi/exec returning a bool

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #46384
| License       | MIT

When using `phpredis` client, the execution of `multi/exec` might return a boolean instead of an array, leading to an error when yielding the result.

Commits
-------

4d2b176 [Cache] Fix dealing with ext-redis' multi/exec returning a bool
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants