Skip to content

Commit b9b607a

Browse files
committed
fix: potential Redis session bug
1 parent b03d645 commit b9b607a

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

system/Session/Handlers/RedisHandler.php

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -297,47 +297,40 @@ protected function lockSession(string $sessionID): bool
297297
// so we need to check here if the lock key is for the
298298
// correct session ID.
299299
if ($this->lockKey === $lockKey) {
300+
// If there is the lock, make the ttl longer.
300301
return $this->redis->expire($this->lockKey, 300);
301302
}
302303

303304
$attempt = 0;
304305

305306
do {
306-
$ttl = $this->redis->ttl($lockKey);
307-
assert(is_int($ttl));
307+
$result = $this->redis->set(
308+
$lockKey,
309+
(string) Time::now()->getTimestamp(),
310+
// NX -- Only set the key if it does not already exist.
311+
// EX seconds -- Set the specified expire time, in seconds.
312+
['nx', 'ex' => 300]
313+
);
308314

309-
if ($ttl > 0) {
310-
sleep(1);
315+
if (! $result) {
316+
usleep(100000);
311317

312318
continue;
313319
}
314320

315-
if (! $this->redis->setex($lockKey, 300, (string) Time::now()->getTimestamp())) {
316-
$this->logger->error(
317-
'Session: Error while trying to obtain lock for ' . $this->keyPrefix . $sessionID
318-
);
319-
320-
return false;
321-
}
322-
323321
$this->lockKey = $lockKey;
324322
break;
325-
} while (++$attempt < 30);
323+
} while (++$attempt < 300);
326324

327-
if ($attempt === 30) {
325+
if ($attempt === 300) {
328326
$this->logger->error(
329-
'Session: Unable to obtain lock for ' . $this->keyPrefix . $sessionID . ' after 30 attempts, aborting.'
327+
'Session: Unable to obtain lock for ' . $this->keyPrefix . $sessionID
328+
. ' after 300 attempts, aborting.'
330329
);
331330

332331
return false;
333332
}
334333

335-
if ($ttl === -1) {
336-
$this->logger->debug(
337-
'Session: Lock for ' . $this->keyPrefix . $sessionID . ' had no TTL, overriding.'
338-
);
339-
}
340-
341334
$this->lock = true;
342335

343336
return true;

0 commit comments

Comments
 (0)