Skip to content

Commit 3459a69

Browse files
committed
Do not remove the handler object if it is created externally
1 parent 44765b0 commit 3459a69

File tree

7 files changed

+39
-14
lines changed

7 files changed

+39
-14
lines changed

src/SaveHandler.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ abstract class SaveHandler implements \SessionHandlerInterface, \SessionUpdateTi
6060
* @var Logger|null
6161
*/
6262
protected ?Logger $logger;
63+
/**
64+
* It says that the handler object was set with an externally created object.
65+
*
66+
* If this is true, the handler object should not be changed or removed (in the close method).
67+
*
68+
* Some functions, such as `session_regenerate_id`, call the close method and then open again!
69+
*
70+
* @var bool
71+
*/
72+
protected bool $setByExternal = false;
6373

6474
/**
6575
* SessionSaveHandler constructor.

src/SaveHandlers/DatabaseHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ protected function prepareConfig(#[SensitiveParameter] array $config) : void
100100

101101
public function setDatabase(Database $database) : static
102102
{
103+
$this->setByExternal = true;
103104
$this->database = $database;
104105
return $this;
105106
}
@@ -280,7 +281,9 @@ public function updateTimestamp($id, $data) : bool
280281
public function close() : bool
281282
{
282283
$closed = !($this->lockId && !$this->unlock());
283-
$this->database = null;
284+
if ($this->setByExternal === false) {
285+
$this->database = null;
286+
}
284287
return $closed;
285288
}
286289

src/SaveHandlers/MemcachedHandler.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ protected function prepareConfig(#[SensitiveParameter] array $config) : void
9494

9595
public function setMemcached(Memcached $memcached) : static
9696
{
97+
$this->setByExternal = true;
9798
$this->memcached = $memcached;
9899
return $this;
99100
}
@@ -229,10 +230,12 @@ public function close() : bool
229230
if ($this->lockId) {
230231
$this->memcached->delete($this->lockId);
231232
}
232-
if (!$this->memcached->quit()) {
233-
return false;
233+
if ($this->setByExternal === false) {
234+
if (!$this->memcached->quit()) {
235+
return false;
236+
}
237+
$this->memcached = null;
234238
}
235-
$this->memcached = null;
236239
return true;
237240
}
238241

src/SaveHandlers/RedisHandler.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ protected function prepareConfig(#[SensitiveParameter] array $config) : void
8080

8181
public function setRedis(Redis $redis) : static
8282
{
83+
$this->setByExternal = true;
8384
$this->redis = $redis;
8485
return $this;
8586
}
@@ -192,19 +193,21 @@ public function close() : bool
192193
if (!isset($this->redis)) {
193194
return true;
194195
}
195-
try {
196-
if ($this->redis->ping()) {
197-
if ($this->lockId) {
198-
$this->redis->del($this->lockId);
199-
}
200-
if (!$this->redis->close()) {
201-
return false;
196+
if ($this->setByExternal === false) {
197+
try {
198+
if ($this->redis->ping()) {
199+
if ($this->lockId) {
200+
$this->redis->del($this->lockId);
201+
}
202+
if (!$this->redis->close()) {
203+
return false;
204+
}
202205
}
206+
} catch (RedisException $e) {
207+
$this->log('Session (redis): Got RedisException on close: ' . $e->getMessage());
203208
}
204-
} catch (RedisException $e) {
205-
$this->log('Session (redis): Got RedisException on close: ' . $e->getMessage());
209+
$this->redis = null;
206210
}
207-
$this->redis = null;
208211
return true;
209212
}
210213

tests/SaveHandlers/DatabaseHandlerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,7 @@ public function testDatabaseSetterAndGetter() : void
188188
$handler->setDatabase($database);
189189
self::assertTrue($handler->open('', ''));
190190
self::assertSame($database, $handler->getDatabase());
191+
self::assertTrue($handler->close());
192+
self::assertSame($database, $handler->getDatabase());
191193
}
192194
}

tests/SaveHandlers/MemcachedHandlerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,7 @@ public function testMemcachedSetterAndGetter() : void
190190
$handler->setMemcached($memcached);
191191
self::assertTrue($handler->open('', ''));
192192
self::assertSame($memcached, $handler->getMemcached());
193+
self::assertTrue($handler->close());
194+
self::assertSame($memcached, $handler->getMemcached());
193195
}
194196
}

tests/SaveHandlers/RedisHandlerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,7 @@ public function testRedisSetterAndGetter() : void
145145
$handler->setRedis($redis);
146146
self::assertTrue($handler->open('', ''));
147147
self::assertSame($redis, $handler->getRedis());
148+
self::assertTrue($handler->close());
149+
self::assertSame($redis, $handler->getRedis());
148150
}
149151
}

0 commit comments

Comments
 (0)