-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Decreasing the probability of race condition in session lock #5170
Decreasing the probability of race condition in session lock #5170
Conversation
Signed-off-by: tianhe1986 <w1s2j3229@163.com>
Signed-off-by: tianhe1986 <w1s2j3229@163.com>
It is unclear how or why this patch improves anything ... You'll need to elaborate a lot. :) |
Take When two requests with same cookie arrive at the same time, the two processes may also execute the code to here simultaneously, and get $ttl = -2. However, |
Ok, that makes sense for Redis, although it can be simplified:
(yes, What about memcache though? |
memecache also could use Although it also has problem, I'll modify the code according to your advice :) |
Signed-off-by: tianhe1986 <w1s2j3229@163.com>
@@ -326,7 +326,11 @@ protected function _get_lock($session_id) | |||
continue; | |||
} | |||
|
|||
if ( ! $this->_memcached->set($lock_key, time(), 300)) | |||
$result = ($this->_memcached->getResultCode() === Memcached::RES_NOTFOUND) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the method params matching ...
$method = ($this->_memcached->getResultCode() === Memcached::RES_NOTFOUND) ? 'add' : 'set';
if ( ! $this->_memcached->$method($lock_key, time(), 300))
$result = ($ttl === -2) | ||
? $this->_redis->set($lock_key, time(), array('nx', 'ex' => 300)) | ||
: $this->_redis->setex($lock_key, 300, time()); | ||
if ( ! $result) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a single empty line prior to this.
Signed-off-by: tianhe1986 <w1s2j3229@163.com>
…ce_condition Decreasing the probability of race condition in session lock
When using
redis
ormemcached
session driver, if a web-client perform multiple requests at the same time, two or more requests may get the session lock, and finally some of them would generate "Session: Error while trying to free lock for" error log.Using
Redis::setnx
andMemcached::add
properly could help to decrease the probability of this race condition.