Uh oh!
There was an error while loading. Please reload this page.
fix: [Session] Redis session race condition - #8323
Merged
Merged
Conversation
kenjis
marked this pull request as draft
December 11, 2023 09:20
kenjisforce-pushed
the
fix-redis-session
branch
from
February 21, 2024 10:10
3fa9be3 to
b9b607aComparekenjis
marked this pull request as ready for review
February 21, 2024 10:25
kenjis
commented
Feb 21, 2024
MemberAuthor
I did simple testing on macOS, and the results was good.
|
kenjisforce-pushed
the
fix-redis-session
branch
from
February 22, 2024 03:47
b9b607a to
7249361CompareMemberAuthor
Explanation of current --- a/system/Session/Handlers/RedisHandler.php+++ b/system/Session/Handlers/RedisHandler.php@@ -293,16 +293,25 @@ RedisHandler::lockSession()
$attempt = 0;
do {
+ // Gets the TTL of the lock record.+ // When there is no lock record, if two processes query Redis at the same time,+ // both will have `$ttl = 0`.
$ttl = $this->redis->ttl($lockKey);
assert(is_int($ttl));
if ($ttl > 0) {
+ // If the TTL is longer than zero second, that is the lock record exists,+ // waits for one second.
sleep(1);
continue;
}
+ // Sets the lock record with TTL 300 seconds.
if (! $this->redis->setex($lockKey, 300, (string) Time::now()->getTimestamp())) {
+ // As long as the Redis server is working properly,+ // the above command will never fail. So the following log will not be recorded.+ // And if two processes have `$ttl = 0`, both will acquire the lock.
$this->logger->error('Session: Error while trying to obtain lock for ' . $this->keyPrefix . $sessionID);
return false;
@@ -334,6 +343,7 @@ RedisHandle::releaseLock()
{
if (isset($this->redis, $this->lockKey) && $this->lock) {
if (! $this->redis->del($this->lockKey)) {
+ // If two processes have the lock, latter one cannot delete the lock record.
$this->logger->error('Session: Error while trying to free lock for ' . $this->lockKey);
return false; |
motoroller1983
commented
Feb 22, 2024
When this fix will be releaed? |
kenjis
commented
Feb 22, 2024
MemberAuthor
As soon as possible when this PR is approved and merged. |
najdanovicivan
commented
Feb 22, 2024
Contributor
@kenjis the only thing I'd add is handling the Redis Exceptions or at least put those in PHPDoc |
kenjisforce-pushed
the
fix-redis-session
branch
from
February 23, 2024 00:52
9c1718b to
a38ac8bComparekenjis
commented
Feb 23, 2024
MemberAuthor
@najdanovicivan Added I also found that we cannot use Redis ACL. If it is a bug, I will fix in this PR. |
kenjisforce-pushed
the
fix-redis-session
branch
2 times, most recently
from
February 23, 2024 06:33
229527b to
a38ac8bCompareThe return value (usually true on success, false on failure) https://www.php.net/manual/en/sessionhandlerinterface.close.php#refsect1-sessionhandlerinterface.close-returnvalues
kenjisforce-pushed
the
fix-redis-session
branch
from
February 23, 2024 06:34
a38ac8b to
2fa55d4CompareMGatner
approved these changes
Feb 23, 2024
kenjis
commented
Feb 23, 2024
MemberAuthor
@najdanovicivan Are you okay to merge this? If so, please approve. |
najdanovicivan
approved these changes
Feb 23, 2024
5 tasks
kenjis
commented
Feb 24, 2024
MemberAuthor
I sent #8578 to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes#4391
Fixes#8567
close()does not returnfalseifreleaseLock()failedread()does not returnfalseiflockSession()failedTesting
I have tested by
ab -n 1000 -c 20.Results:
Details
develop:
This PR:
Checklist: