Skip to content
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

Redis Connection database change to 0 when throw RedisException(read error on connection) #41651

Closed
kkdaysamtang opened this issue Mar 24, 2022 · 1 comment

Comments

@kkdaysamtang
Copy link
Contributor

  • Laravel Version: 9.5.1
  • PHP Version: 8.0.13
  • Database Driver & Version: not use
  • Redis Driver & Version: phpredis & 5.0.6

Description:

Redis connection use the datebase 1 and set read_timeout to 1 second, if the Redis connection fails with read_timeout (RedisException), the using database change to 0

local.ERROR: read error on connection to redis:6379 {"exception":"[object] (RedisException(code: 0): read error on connection to redis:6379 at /var/www/laravel/vendor/laravel/framework/src/Illuminate/Redis/Connections/Connection.php:116)

Effect two scenarios

  • Cache: If this error occurs, the next redis command will execute on database 0
  • Queue Jobs: when this error occurs, queue worker still looping and worker pop jobs from the database 0, but jobs are all queued on the database 1

this issue is similar to #41302

Steps To Reproduce:

config/database.php

    ...
    'redis' => [
        ...
        'example' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD'),
            'port' => env('REDIS_PORT', '6379'),
            'database' => 1,
            'read_timeout' => 1
        ],
        ...

example

    public function testReadTimeoutIssue()
    {
        /**
         * $targetConn use the database 1
         * $defaultConn use the database 0
         */
        $targetConn = Redis::connection('example');
        $defaultConn = Redis::connection();

        try {
            /**
             * If the Redis connection fails with read_timeout,
             * Redis connection database change to 0.
             * use blpop to trigger read_timeout RedisException
             */
            $targetConn->blpop('pop_key', 2);
        } catch (\Throwable $e) {
            // $e is RedisException with message read error on connection to redis:6379
        }

        $message = 'some data';
        /**
         * $targetConn database change to 0
         * this test_key will set on the database 0
         */
        $targetConn->set('test_key', $message);

        /**
         * Default connection(database:0)
         * use default connection get this key
         */
        $this->assertEquals($message, $defaultConn->get('test_key'));
    }
@driesvints
Copy link
Member

A fix was merged, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants