Skip to content

Automatic reconnect behavior inconsistent #1757

Closed

Description

@johannes85

Expected behaviour

When automatically reconnect the auth command should always be send.

Actual behaviour

When the server wents away the reconnect sends the auth command, but when reconnecting after calling the close method the auth command isn't sent.

I'm seeing this behaviour on

  • OS: Debian Jessie
  • Redis: 6.0.1
  • PHP: 7.2.30
  • phpredis: 5.2.2

Steps to reproduce, backtrace or example script

Works:

$client = new \Redis();
$client->connect('127.0.0.1', 6379);
$client->auth('foo');
$client->set('foo', 'bar');
sleep(10);  // Restarting redis server while script is waiting
$client->set('foo', 'bar');

Isn't working, produces PHP Fatal error: Uncaught RedisException: NOAUTH Authentication required.

$client = new \Redis();
$client->connect('127.0.0.1', 6379);
$client->auth('foo');
$client->set('foo', 'bar');
$client->close();
$client->set('foo', 'bar');

I've checked

  • There is no similar issue from other users
  • Issue isn't fixed in develop branch

Activity

  1. Venorcis commented on Jun 17, 2020

    @Venorcis

    I have the same problem, though in a long running script on a replica where (apparently) a connection reset sometimes occurs. After this happened I get the same 'NOAUTH Authentication required' error.

  2. xiaoliwang commented on Jul 21, 2020

    @xiaoliwang

    I have the same problem. Only if I catch the exception and close the connection, the error 'NOAUTH AUTHENTICATION required' disappear.

  3. michael-grunder commented on Jul 21, 2020

    @michael-grunder
    Member

    I can't replicate this with PhpRedis 5.3.0. Both of the examples work fine (and reauthorize the connection)

  4. xiaoliwang commented on Jul 22, 2020

    @xiaoliwang
    class RedisBase extends Redis	
    	public function __construct()
        {
            $this->connect('127.0.0.1', 6379);
            $this->auth('password');
        }
    }
    
    new RedisBase();

    When i use tcpkill to kill the tcp connection, I get the 'NOAUTH Authentication required' throwed by connect function.

  5. michael-grunder commented on Jul 22, 2020

    @michael-grunder
    Member

    Can you provide a bit more context?

    From the above sample code, it seems like you'd have a difficult time timing the tcpkill call. Or do you mean you're calling it in a loop?

  6. xiaoliwang commented on Jul 23, 2020

    @xiaoliwang

    Well. I use php 7.3.6 And php-redis 5.3.0
    Use nginx and php-fpm

    I use the script as below, and I can get the right value.

    <?php
    $redis = new Redis();
    $redis->pconnect('127.0.0.1');
    $redis->auth('password');
    echo $redis->get('a');

    After that, I use tcpkill to kill the connection. And then stop tcpkill running.
    Then I visit the page, I will get the error below.

    Stack trace:
    #0 /home/www/missevan-web/public/index_temp.php(3): Redis->pconnect('r-bp1bts3fbiqme...')
    #1 {main}
    thrown in /home/www/missevan-web/public/index_temp.php on line 3
    NOTICE: PHP message: PHP Fatal error: Uncaught RedisException: NOAUTH Authentication required. in /home/www/missevan-web/public/index_temp.php:3

    And then I do the samething to the script below. It's OK. No error appear.

    <?php
    $redis = new Redis();
    try{
      $redis->pconnect('127.0.0.1');
      $redis->auth('password');
      echo $redis->get('a');
    } catch(Exception $e) {
      $redis->close();
    }
  7. michael-grunder commented on Jul 23, 2020

    @michael-grunder
    Member

    Ok I will try that setup tomorrow morning.

  8. michael-grunder commented on Jul 23, 2020

    @michael-grunder
    Member

    I can replicate this. I'll post an update when I figure out exactly what's going on.

  9. michael-grunder commented on Jul 23, 2020

    @michael-grunder
    Member

    OK, this exception is being thrown in our ECHO "liveness check".

    There are two workarounds (as long as you're running >= 5.3.0):

    1. Change your pconnect call to include the AUTH information:
    $redis->pconnect('127.0.0.1', 9999, 1, NULL, 0, 0, ['auth' => ['password']]);
    1. Disable the redis.pconnect.echo_check_liveness INI setting. I don't recommend this if you've ever had persistent connections get into a bad state. If this has never happened to you then it's probably OK.
  10. JimmyBanks commented on Jul 23, 2020

    @JimmyBanks

    Thanks @michael-grunder

    Will this essentially be a necessity to fix the issue for persistent connections, and the best practice moving forward? Or is the underlying issue with the auth() method also fixable in a future version?

  11. michael-grunder commented on Jul 23, 2020

    @michael-grunder
    Member

    Once persistent connections are fixed we won't need the liveness check at all, so the problem will go away.

    That said, this specific issue is fixable before that, because in actuality a NOAUTH error is sufficient in and of itself to confirm "liveness". The whole point of the liveness check is to make sure there aren't unconsumed replies on the connection when we pull it from the persistent connection pool. If Redis tells us NOAUTH we know for a fact there aren't any replies since we're not even logged in.

    I think it will be safe to add NOAUTH as a "success" for the purposes of the liveness check, which should also be pretty simple to implement.

  12. xiaoliwang commented on Jul 24, 2020

    @xiaoliwang

    Thanks @michael-grunder 。I will try。

  13. michael-grunder commented on Oct 31, 2025

    @michael-grunder
    Member

    If I'm reading the issue correctly this was fixed in d8c7f7a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions