From e419355929b26ff1fb47b2759b7c1357b1dfcd0c Mon Sep 17 00:00:00 2001 From: Hugues Alary Date: Thu, 26 Oct 2017 15:46:28 -0700 Subject: [PATCH] Fix deadlock when using `session_start(array("read_and_close"=>true))` --- src/Cm/RedisSession/Handler.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Cm/RedisSession/Handler.php b/src/Cm/RedisSession/Handler.php index 3d34aff..0154f96 100644 --- a/src/Cm/RedisSession/Handler.php +++ b/src/Cm/RedisSession/Handler.php @@ -666,6 +666,21 @@ public function destroy($sessionId) */ public function close() { + $id = session_id(); + $sessionId = 'sess_' . $id; + + if ( ! $this->_useLocking + || ( ! ($pid = $this->_redis->hGet('sess_'.$sessionId, 'pid')) || $pid == $this->_getPid()) + ) { + $this->_redis->pipeline() + ->select($this->_dbNum) + ->hMSet($sessionId, array( + 'lock' => 0, // 0 so that next lock attempt will get 1 + )) + ->expire($sessionId, min($this->getLifeTime(), $this->_maxLifetime)) + ->exec(); + } + $this->_log("Closing connection"); if ($this->_redis) $this->_redis->close(); return true;