diff options
author | Andrey Andreev <narf@devilix.net> | 2017-07-06 10:49:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-06 10:49:13 +0200 |
commit | 4bda6f7fdbead56108c4377ae53db8089f10d933 (patch) | |
tree | a899e7ae6d194e017f7839787df1a2972afdd0a1 /system | |
parent | 5b5e053b95a586c3c278e8d7c9d55c2459ed0908 (diff) | |
parent | 24e91311bd86b9eddb9ffb2d4b6ab78bf19796c2 (diff) |
[ci skip] Merge pull request #5170 from tianhe1986/develop_session_race_condition
Decreasing the probability of race condition in session lock
Diffstat (limited to 'system')
-rw-r--r-- | system/libraries/Session/drivers/Session_memcached_driver.php | 5 | ||||
-rw-r--r-- | system/libraries/Session/drivers/Session_redis_driver.php | 6 |
2 files changed, 8 insertions, 3 deletions
diff --git a/system/libraries/Session/drivers/Session_memcached_driver.php b/system/libraries/Session/drivers/Session_memcached_driver.php index 2556bf0f7..5e90539d7 100644 --- a/system/libraries/Session/drivers/Session_memcached_driver.php +++ b/system/libraries/Session/drivers/Session_memcached_driver.php @@ -310,7 +310,7 @@ class CI_Session_memcached_driver extends CI_Session_driver implements SessionHa if ( ! $this->_memcached->replace($this->_lock_key, time(), 300)) { return ($this->_memcached->getResultCode() === Memcached::RES_NOTFOUND) - ? $this->_memcached->set($this->_lock_key, time(), 300) + ? $this->_memcached->add($this->_lock_key, time(), 300) : FALSE; } } @@ -326,7 +326,8 @@ class CI_Session_memcached_driver extends CI_Session_driver implements SessionHa continue; } - if ( ! $this->_memcached->set($lock_key, time(), 300)) + $method = ($this->_memcached->getResultCode() === Memcached::RES_NOTFOUND) ? 'add' : 'set'; + if ( ! $this->_memcached->$method($lock_key, time(), 300)) { log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id); return FALSE; diff --git a/system/libraries/Session/drivers/Session_redis_driver.php b/system/libraries/Session/drivers/Session_redis_driver.php index 2aeb77267..76bddffd3 100644 --- a/system/libraries/Session/drivers/Session_redis_driver.php +++ b/system/libraries/Session/drivers/Session_redis_driver.php @@ -365,7 +365,11 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle continue; } - if ( ! $this->_redis->setex($lock_key, 300, time())) + $result = ($ttl === -2) + ? $this->_redis->set($lock_key, time(), array('nx', 'ex' => 300)) + : $this->_redis->setex($lock_key, 300, time()); + + if ( ! $result) { log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id); return FALSE; |