Skip to content

Commit b307950

Browse files
committed
fix(Session): release lock state on close and check session ID in MemcachedHandler
1 parent 59e5eea commit b307950

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎system/Session/Handlers/MemcachedHandler.php‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ public function write($id, $data): bool
221221
public function close(): bool
222222
{
223223
if (isset($this->memcached)) {
224-
if (isset($this->lockKey)) {
225-
$this->memcached->delete($this->lockKey);
224+
if (isset($this->lockKey) && ! $this->releaseLock()) {
225+
return false;
226226
}
227227

228228
return true;
@@ -265,11 +265,15 @@ public function gc($max_lifetime): int
265265
*/
266266
protected function lockSession(string $sessionID): bool
267267
{
268-
if (isset($this->lockKey)) {
268+
$lockKey = $this->keyPrefix . $sessionID . ':lock';
269+
270+
// PHP 7 reuses the SessionHandler object on regeneration,
271+
// so we need to check here if the lock key is for the
272+
// correct session ID.
273+
if ($this->lockKey === $lockKey) {
269274
return $this->memcached->replace($this->lockKey, Time::now()->getTimestamp(), 300);
270275
}
271276

272-
$lockKey = $this->keyPrefix . $sessionID . ':lock';
273277
$attempt = 0;
274278

275279
do {

‎tests/system/Session/Handlers/MemcachedHandlerTest.php‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,48 @@ public function testGC(): void
128128
$handler = $this->getInstance();
129129
$this->assertSame(1, $handler->gc(3600));
130130
}
131+
132+
public function testReopenAndReadWithSameHandlerInstance(): void
133+
{
134+
$handler = $this->getInstance();
135+
$handler->open($this->sessionSavePath, $this->sessionName);
136+
137+
$sessionId = '666666b43phsnnf8if6bo33b635e4448';
138+
139+
$this->assertSame('', $handler->read($sessionId));
140+
141+
$data = '__ci_last_regenerate|i:1664607454;key|s:5:"value";';
142+
$this->assertTrue($handler->write($sessionId, $data));
143+
$this->assertTrue($handler->close());
144+
145+
// Reopen with the same handler instance (e.g. session_write_close() + session_start())
146+
$this->assertTrue($handler->open($this->sessionSavePath, $this->sessionName));
147+
$this->assertSame($data, $handler->read($sessionId));
148+
$this->assertTrue($handler->close());
149+
}
150+
151+
public function testRegenerateSessionIdWithSameHandlerInstance(): void
152+
{
153+
$handler = $this->getInstance();
154+
$handler->open($this->sessionSavePath, $this->sessionName);
155+
156+
$oldSessionId = '777777b43phsnnf8if6bo33b635e4449';
157+
$newSessionId = '888888b43phsnnf8if6bo33b635e4450';
158+
159+
$this->assertSame('', $handler->read($oldSessionId));
160+
161+
$data = '__ci_last_regenerate|i:1664607454;role|s:5:"admin";';
162+
$this->assertTrue($handler->write($oldSessionId, $data));
163+
$this->assertTrue($handler->close());
164+
165+
// PHP reuses the same SessionHandler object on regeneration
166+
$this->assertTrue($handler->open($this->sessionSavePath, $this->sessionName));
167+
$this->assertSame('', $handler->read($newSessionId));
168+
$this->assertTrue($handler->write($newSessionId, $data));
169+
$this->assertTrue($handler->close());
170+
171+
$this->assertTrue($handler->open($this->sessionSavePath, $this->sessionName));
172+
$this->assertSame($data, $handler->read($newSessionId));
173+
$this->assertTrue($handler->close());
174+
}
131175
}

0 commit comments

Comments
 (0)