Skip to content

Commit 32eed80

Browse files
committed
fix(Session): release previous lock before acquiring new one in MemcachedHandler
1 parent 34aa992 commit 32eed80

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎system/Session/Handlers/MemcachedHandler.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ protected function lockSession(string $sessionID): bool
270270
return $this->memcached->replace($this->lockKey, Time::now()->getTimestamp(), 300);
271271
}
272272

273+
if (isset($this->lockKey) && ! $this->releaseLock()) {
274+
return false;
275+
}
276+
273277
$attempt = 0;
274278

275279
do {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,31 @@ public function testRegenerateSessionIdWriteWithoutClose(): void
231231
$this->assertSame($newData, $handler2->read($newSessionId));
232232
$this->assertTrue($handler2->close());
233233
}
234+
235+
public function testReadDifferentSessionIdsWithoutCloseReleasesPreviousLock(): void
236+
{
237+
$handler = $this->getInstance();
238+
$handler->open($this->sessionSavePath, $this->sessionName);
239+
240+
$firstSessionId = $this->generateSessionId('bbbbbb');
241+
$secondSessionId = $this->generateSessionId('cccccc');
242+
243+
$firstData = '__ci_last_regenerate|i:1664607454;user|s:5:"alice";';
244+
$this->assertSame('', $handler->read($firstSessionId));
245+
$this->assertTrue($handler->write($firstSessionId, $firstData));
246+
$this->assertTrue($handler->close());
247+
248+
// Read two different session IDs on the same handler without closing between them
249+
$this->assertTrue($handler->open($this->sessionSavePath, $this->sessionName));
250+
$this->assertSame($firstData, $handler->read($firstSessionId));
251+
$this->assertSame('', $handler->read($secondSessionId));
252+
253+
// Another handler should be able to acquire the lock for the first session ID
254+
$handler2 = $this->getInstance();
255+
$this->assertTrue($handler2->open($this->sessionSavePath, $this->sessionName));
256+
$this->assertSame($firstData, $handler2->read($firstSessionId));
257+
$this->assertTrue($handler2->close());
258+
259+
$this->assertTrue($handler->close());
260+
}
234261
}

0 commit comments

Comments
 (0)