Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions system/Session/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public function __construct(SessionConfig $config, string $ipAddress)
$this->keyPrefix .= $this->ipAddress . ':';
}

$this->lockRetryInterval = $config->lockWait ?? $this->lockRetryInterval;
$this->lockMaxRetries = $config->lockAttempts ?? $this->lockMaxRetries;
$this->lockRetryInterval = $config->lockRetryInterval;
$this->lockMaxRetries = $config->lockMaxRetries;
Comment thread
gr8man marked this conversation as resolved.
Resolved
}

protected function setSavePath(): void
Expand Down Expand Up @@ -386,10 +386,10 @@ protected function lockSession(string $sessionID): bool
break;
} while (++$attempt < $this->lockMaxRetries);

if ($attempt === 300) {
if ($attempt === $this->lockMaxRetries) {
$this->logger->error(
'Session: Unable to obtain lock for ' . $this->keyPrefix . $sessionID
. ' after 300 attempts, aborting.',
. ' after ' . $this->lockMaxRetries . ' attempts, aborting.',
);

return false;
Expand Down
29 changes: 28 additions & 1 deletion tests/system/Session/Handlers/Database/RedisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use CodeIgniter\Session\Handlers\RedisHandler;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\TestLogger;
use Config\Logger as LoggerConfig;
use Config\Session as SessionConfig;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
Expand Down Expand Up @@ -54,7 +56,10 @@ protected function getInstance($options = []): RedisHandler
$sessionConfig->{$key} = $value;
}

return new RedisHandler($sessionConfig, $this->userIpAddress);
$handler = new RedisHandler($sessionConfig, $this->userIpAddress);
$handler->setLogger(new TestLogger(new LoggerConfig()));

return $handler;
}

protected function tearDown(): void
Expand Down Expand Up @@ -359,4 +364,26 @@ public function testResetPersistentConnections(): void
$handler1->close();
$handler2->close();
}

public function testLockMaxRetries(): void
{
$options = [
'lockRetryInterval' => 10_000, // 10ms
'lockMaxRetries' => 3,
];

$handler1 = $this->getInstance($options);
$handler1->open($this->sessionSavePath, $this->sessionName);
$handler1->read('lock_test_session'); // Acquires lock

$handler2 = $this->getInstance($options);
$handler2->open($this->sessionSavePath, $this->sessionName);

// Before the fix, this would incorrectly return true (since $attempt === 3 !== 300).
// With the fix, it should return false after 3 attempts.
$this->assertFalse($handler2->read('lock_test_session'));

$handler1->close();
$handler2->close();
}
}
12 changes: 1 addition & 11 deletions utils/phpstan-baseline/property.notFound.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 49 errors
# total 47 errors

parameters:
ignoreErrors:
Expand All @@ -17,16 +17,6 @@ parameters:
count: 14
path: ../../system/Database/SQLSRV/Forge.php

-
message: '#^Access to an undefined property Config\\Session\:\:\$lockAttempts\.$#'
count: 1
path: ../../system/Session/Handlers/RedisHandler.php

-
message: '#^Access to an undefined property Config\\Session\:\:\$lockWait\.$#'
count: 1
path: ../../system/Session/Handlers/RedisHandler.php

-
message: '#^Access to an undefined property CodeIgniter\\Database\\BaseConnection\:\:\$mysqli\.$#'
count: 1
Expand Down
Loading