Skip to content

Commit 44aa31b

Browse files
committed
Fix session destroy error in Swoole coroutine context
- Override destroy() method in CoroutineSession to avoid calling native session_destroy() - Properly delete session data from Redis and clear session state - Prevents 'Trying to destroy uninitialized session' error during logout
1 parent 4fff564 commit 44aa31b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/Session/CoroutineSession.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,32 @@ public function close()
115115
$this->deferCoroutineId = -1;
116116
}
117117

118+
public function destroy()
119+
{
120+
if (!$this->getIsActive()) {
121+
return;
122+
}
123+
124+
// Remove session data from Redis
125+
if ($this->_sessionId !== null) {
126+
try {
127+
$sessionKey = $this->calculateKey($this->getId());
128+
$this->redis->del($sessionKey);
129+
} catch (\Throwable $e) {
130+
\Yii::error('Failed to destroy session: ' . $e->getMessage(), __METHOD__);
131+
}
132+
}
133+
134+
// Clear session data
135+
if (isset($_SESSION) && is_array($_SESSION)) {
136+
$_SESSION = [];
137+
}
138+
139+
// Reset session ID
140+
$this->_sessionId = null;
141+
$this->isClosed = true;
142+
}
143+
118144
public function reset(): void
119145
{
120146
$this->close();

0 commit comments

Comments
 (0)