From 7ff7c082b86f44d2ce80cfe4606f6dcc22fa2de5 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sun, 8 Feb 2015 16:59:44 +0100 Subject: [PATCH] Work-around for a race condition with parallel session initialization --- .../system/session/SessionHandler.class.php | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/wcfsetup/install/files/lib/system/session/SessionHandler.class.php b/wcfsetup/install/files/lib/system/session/SessionHandler.class.php index 0337999cc1..b5e156d35c 100644 --- a/wcfsetup/install/files/lib/system/session/SessionHandler.class.php +++ b/wcfsetup/install/files/lib/system/session/SessionHandler.class.php @@ -514,7 +514,33 @@ class SessionHandler extends SingletonFactory { ); if ($spiderID !== null) $sessionData['spiderID'] = $spiderID; - $this->session = call_user_func(array($this->sessionEditorClassName, 'create'), $sessionData); + + try { + $this->session = call_user_func(array($this->sessionEditorClassName, 'create'), $sessionData); + } + catch (DatabaseException $e) { + // MySQL error 23000 = unique key + // do not check against the message itself, some weird systems localize them + if ($e->getCode() == 23000 && $this->supportsVirtualSessions) { + // find existing session + $session = call_user_func(array($this->sessionClassName, 'getSessionByUserID'), $this->user->userID); + + if ($session === null) { + // MySQL reported a unique key error, but no corresponding session exists, rethrow exception + throw $e; + } + else { + // inherit existing session + $this->session = $session; + $this->loadVirtualSession(true); + } + } + else { + // unrelated to user id + throw $e; + } + } + $this->firstVisit = true; $this->loadVirtualSession(true); } -- 2.20.1