From d388388dbe5e99560e164172ce11b9e5d8cc2f32 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Fri, 1 Sep 2023 03:31:50 +0800 Subject: [PATCH 1/2] fix: The close function not only delete the lock from redis but also need to clear the variable value. --- system/Session/Handlers/RedisHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Session/Handlers/RedisHandler.php b/system/Session/Handlers/RedisHandler.php index a85a16c68ca5..815e3644984b 100644 --- a/system/Session/Handlers/RedisHandler.php +++ b/system/Session/Handlers/RedisHandler.php @@ -223,7 +223,7 @@ public function close(): bool if (($pingReply === true) || ($pingReply === '+PONG')) { if (isset($this->lockKey)) { - $this->redis->del($this->lockKey); + $this->releaseLock(); } if (! $this->redis->close()) { From 2b33a11b13c1ad95691f17719812f8054dbfe74a Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Fri, 1 Sep 2023 12:42:44 +0800 Subject: [PATCH 2/2] test: Add the testcase for the case. --- .../Handlers/Database/RedisHandlerTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/system/Session/Handlers/Database/RedisHandlerTest.php b/tests/system/Session/Handlers/Database/RedisHandlerTest.php index d7acb0dbf56d..94d17461d243 100644 --- a/tests/system/Session/Handlers/Database/RedisHandlerTest.php +++ b/tests/system/Session/Handlers/Database/RedisHandlerTest.php @@ -168,4 +168,26 @@ public function testGC(): void $handler = $this->getInstance(); $this->assertSame(1, $handler->gc(3600)); } + + /** + * See https://github.com/codeigniter4/CodeIgniter4/issues/7695 + */ + public function testSecondaryReadAfterClose(): void + { + $handler = $this->getInstance(); + $handler->open($this->sessionSavePath, $this->sessionName); + + $expected = <<<'DATA' + __ci_last_regenerate|i:1664607454;_ci_previous_url|s:32:"http://localhost:8080/index.php/";key|s:5:"value"; + DATA; + $this->assertSame($expected, $handler->read('555556b43phsnnf8if6bo33b635e4447')); + + $handler->close(); + + $handler->open($this->sessionSavePath, $this->sessionName); + + $this->assertSame($expected, $handler->read('555556b43phsnnf8if6bo33b635e4447')); + + $handler->close(); + } }