From 2fbe1fa8e6515f603ac788f2f49c83ba24f485eb Mon Sep 17 00:00:00 2001 From: Michael Long Date: Sat, 2 Mar 2019 18:36:17 +0800 Subject: Resolve race condition in redis driven session key get_lock --- system/libraries/Session/drivers/Session_redis_driver.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'system') diff --git a/system/libraries/Session/drivers/Session_redis_driver.php b/system/libraries/Session/drivers/Session_redis_driver.php index 07511e555..7262a2300 100644 --- a/system/libraries/Session/drivers/Session_redis_driver.php +++ b/system/libraries/Session/drivers/Session_redis_driver.php @@ -389,8 +389,9 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle if ( ! $result) { - log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id); - return FALSE; + // Sleep for 0.1s to wait for lock releases. + usleep(100000); + continue; } $this->_lock_key = $lock_key; -- cgit v1.2.3-24-g4f1b From 7f8e0cd333176ee943e0c46eb9fe10d31b70d3ce Mon Sep 17 00:00:00 2001 From: Michael Long Date: Fri, 15 Mar 2019 10:47:49 +0800 Subject: Refactor behaviors with different ttl of lock_key --- system/libraries/Session/drivers/Session_redis_driver.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'system') diff --git a/system/libraries/Session/drivers/Session_redis_driver.php b/system/libraries/Session/drivers/Session_redis_driver.php index 7262a2300..c9a98b822 100644 --- a/system/libraries/Session/drivers/Session_redis_driver.php +++ b/system/libraries/Session/drivers/Session_redis_driver.php @@ -383,16 +383,16 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle continue; } - $result = ($ttl === -2) - ? $this->_redis->set($lock_key, time(), array('nx', 'ex' => 300)) - : $this->_redis->setex($lock_key, 300, time()); - - if ( ! $result) + if ($ttl === -2 && ! $this->_redis->set($lock_key, time(), array('nx', 'ex' => 300))) { - // Sleep for 0.1s to wait for lock releases. - usleep(100000); + // Sleep for 1s to wait for lock releases. + sleep(1); continue; } + elseif ($ttl === -1 && ! $this->_redis->setex($lock_key, 300, time())) { + log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id); + return FALSE; + } $this->_lock_key = $lock_key; break; -- cgit v1.2.3-24-g4f1b From d3bdd3c973508ff89e6f6789521f2e2476099cfa Mon Sep 17 00:00:00 2001 From: Michael Long Date: Fri, 15 Mar 2019 11:38:30 +0800 Subject: Fix code style --- system/libraries/Session/drivers/Session_redis_driver.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'system') diff --git a/system/libraries/Session/drivers/Session_redis_driver.php b/system/libraries/Session/drivers/Session_redis_driver.php index c9a98b822..08b299f9c 100644 --- a/system/libraries/Session/drivers/Session_redis_driver.php +++ b/system/libraries/Session/drivers/Session_redis_driver.php @@ -389,7 +389,8 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle sleep(1); continue; } - elseif ($ttl === -1 && ! $this->_redis->setex($lock_key, 300, time())) { + elseif ($ttl === -1 && ! $this->_redis->setex($lock_key, 300, time())) + { log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id); return FALSE; } -- cgit v1.2.3-24-g4f1b From 06739bdc05f2fa5fc92db0675ec48f7374ae50cf Mon Sep 17 00:00:00 2001 From: Michael Long Date: Sat, 16 Mar 2019 00:17:06 +0800 Subject: Remove unnecessary code --- system/libraries/Session/drivers/Session_redis_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system') diff --git a/system/libraries/Session/drivers/Session_redis_driver.php b/system/libraries/Session/drivers/Session_redis_driver.php index 08b299f9c..c90e94bd3 100644 --- a/system/libraries/Session/drivers/Session_redis_driver.php +++ b/system/libraries/Session/drivers/Session_redis_driver.php @@ -389,7 +389,7 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle sleep(1); continue; } - elseif ($ttl === -1 && ! $this->_redis->setex($lock_key, 300, time())) + elseif ( ! $this->_redis->setex($lock_key, 300, time())) { log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id); return FALSE; -- cgit v1.2.3-24-g4f1b