SessionManager: Ignore Session object destruction during global shutdown
authorBrad Jorsch <bjorsch@wikimedia.org>
Wed, 20 Apr 2016 16:27:26 +0000 (12:27 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Wed, 20 Apr 2016 17:16:40 +0000 (13:16 -0400)
We already save all open SessionBackends when shutdown handlers are run,
which *should* make the Session object destructors that run during
global shutdown not have anything to save. But it can get fooled if the
Session data contains other objects that have already gotten destroyed
during the global shutdown, leading to spurious warnings and errors as
it tries to access partically-destroyed objects.

The solution is to set a flag when we do the shutdown handlers and just
ignore the last gasps from Session::__destruct() that might come after.

Change-Id: Ic3eb0bac2d29a30488c84b6525ad796a7f1c9ce9

includes/session/SessionBackend.php
includes/session/SessionManager.php
tests/phpunit/includes/session/SessionManagerTest.php

index 2626aa8..264e1ae 100644 (file)
@@ -94,6 +94,8 @@ final class SessionBackend {
        private $usePhpSessionHandling = true;
        private $checkPHPSessionRecursionGuard = false;
 
+       private $shutdown = false;
+
        /**
         * @param SessionId $id Session ID object
         * @param SessionInfo $info Session info to populate from
@@ -181,12 +183,21 @@ final class SessionBackend {
         */
        public function deregisterSession( $index ) {
                unset( $this->requests[$index] );
-               if ( !count( $this->requests ) ) {
+               if ( !$this->shutdown && !count( $this->requests ) ) {
                        $this->save( true );
                        $this->provider->getManager()->deregisterSessionBackend( $this );
                }
        }
 
+       /**
+        * Shut down a session
+        * @private For use by \MediaWiki\Session\SessionManager::shutdown() only
+        */
+       public function shutdown() {
+               $this->save( true );
+               $this->shutdown = true;
+       }
+
        /**
         * Returns the session ID.
         * @return string
index 29cd69a..a364045 100644 (file)
@@ -626,7 +626,7 @@ final class SessionManager implements SessionManagerInterface {
                        }
                        // @codeCoverageIgnoreEnd
                        foreach ( $this->allSessionBackends as $backend ) {
-                               $backend->save( true );
+                               $backend->shutdown();
                        }
                }
        }
index 6218f0a..d04d7ec 100644 (file)
@@ -751,8 +751,8 @@ class SessionManagerTest extends MediaWikiTestCase {
                $manager = \TestingAccessWrapper::newFromObject( $this->getManager() );
                $manager->setLogger( new \Psr\Log\NullLogger() );
 
-               $mock = $this->getMock( 'stdClass', [ 'save' ] );
-               $mock->expects( $this->once() )->method( 'save' );
+               $mock = $this->getMock( 'stdClass', [ 'shutdown' ] );
+               $mock->expects( $this->once() )->method( 'shutdown' );
 
                $manager->allSessionBackends = [ $mock ];
                $manager->shutdown();