CodeIgniter Forums
Losing session while using redis Driver - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: Losing session while using redis Driver (/showthread.php?tid=71928)



Losing session while using redis Driver - gmedel - 10-12-2018

Hi guys,
I'm having problems with the redis driver and wanted to know if anyone has experienced the same issues

Platform: Codeigniter 3.1.9 PHP 7.2.1

If I try to make many subsequent AJAX calls I usually lose the session.

For example I try to call 10 different endpoints in subsequent calls. 
At least 1 of the calls will fail everytime when I use the redis driver, this does not happen if I'm using files for storing the session data.

It's not that the session is lost (if I open another tab and visit a page that's not ajax heavy I'll still be logged in)
But for an unknown reason codeigniter is not able to retrieve the session when it's making that many calls while using the redis driver

Has anyone experienced this issue?

Thanks a lot in advance


RE: Losing session while using redis Driver - tgix - 10-16-2018

Just found this myself when updating 3.1.5 -> 3.1.9. I will try doing incremental updates and see when it breaks.


RE: Losing session while using redis Driver - InsiteFX - 10-16-2018

You need to call the session_write_close()

Long story short - call session_write_close() once you no longer need anything to do with session variables.


RE: Losing session while using redis Driver - tgix - 10-16-2018

Yeah session_write_close() is needed and already implemented. 
However, while the 3.1.6 patch is correctly using the "SET resource-name anystring NX EX max-lock-time" from Redis home page https://redis.io/commands/set I'd say that the code is wrong. It is wrong because from 3.1.6, no retries are being made but instead there is a return FALSE after the failed call to $this->_redis->set() shouldn't that be a continue?


PHP Code:
$attempt 0;
do
{
    if ((
$ttl $this->_redis->ttl($lock_key)) > 0)
    {
        
sleep(1);
        continue;
    }

    
$result = ($ttl === -2)
        ? 
$this->_redis->set($lock_keytime(), array('nx''ex' => 300))
        : 
$this->_redis->setex($lock_key300time());

    if ( ! 
$result)
    {
        
log_message('error''Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id);
        return 
FALSE// THIS SHOULD BE continue TO ALLOW UP TO 30 ATTEMPTS TO GET THE LOCK?
    
}

    
$this->_lock_key $lock_key;
    break;
}
while (++
$attempt 30); 



RE: Losing session while using redis Driver - gmedel - 10-18-2018

(10-16-2018, 04:43 AM)InsiteFX Wrote: You need to call the session_write_close()

Long story short - call session_write_close() once you no longer need anything to do with session variables.


session_write_close() is being called.


I'm pretty sure this is a redis driver issue, since it works absolutely fine with the files driver.

But thanks!


RE: Losing session while using redis Driver - gmedel - 10-18-2018

(10-16-2018, 03:31 AM)tgix Wrote: Just found this myself when updating 3.1.5 -> 3.1.9. I will try doing incremental updates and see when it breaks.


I was pretty sure that it was a drivers issue, thank you for confirming it