Make SessionBackend::save() update the user post-send
authorAaron Schulz <aschulz@wikimedia.org>
Sun, 11 Sep 2016 19:52:11 +0000 (12:52 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 12 Oct 2016 18:14:45 +0000 (11:14 -0700)
Bug: T92357
Change-Id: Id4f4991aca1ceeb74b59e980f09863041246a4fc

includes/MediaWiki.php
includes/deferred/DeferredUpdates.php
includes/session/SessionBackend.php

index 8cf009f..218337a 100644 (file)
@@ -899,6 +899,7 @@ class MediaWiki {
 
                // Do any deferred jobs
                DeferredUpdates::doUpdates( 'enqueue' );
+               DeferredUpdates::setImmediateMode( true );
 
                // Make sure any lazy jobs are pushed
                JobQueueGroup::pushLazyJobs();
index 8a761f5..1ba6c1f 100644 (file)
@@ -52,6 +52,8 @@ class DeferredUpdates {
        private static $preSendUpdates = [];
        /** @var DeferrableUpdate[] Updates to be deferred until after request end */
        private static $postSendUpdates = [];
+       /** @var bool Whether to just run updates in addUpdate() */
+       private static $immediateMode = false;
 
        const ALL = 0; // all updates; in web requests, use only after flushing the output buffer
        const PRESEND = 1; // for updates that should run before flushing output buffer
@@ -85,6 +87,12 @@ class DeferredUpdates {
                        self::push( self::$postSendUpdates, $update );
                }
 
+               if ( self::$immediateMode ) {
+                       // No more explicit doUpdates() calls will happen, so run this now
+                       self::doUpdates( 'run' );
+                       return;
+               }
+
                // Try to run the updates now if in CLI mode and no transaction is active.
                // This covers scripts that don't/barely use the DB but make updates to other stores.
                if ( $wgCommandLineMode ) {
@@ -126,6 +134,14 @@ class DeferredUpdates {
                }
        }
 
+       /**
+        * @param bool $value Whether to just immediately run updates in addUpdate()
+        * @since 1.28
+        */
+       public static function setImmediateMode( $value ) {
+               self::$immediateMode = (bool)$value;
+       }
+
        /**
         * @param DeferrableUpdate[] $queue
         * @param DeferrableUpdate $update
index 263cb11..ea811b6 100644 (file)
@@ -642,7 +642,11 @@ final class SessionBackend {
                        ] );
                        $this->user->setToken();
                        if ( !wfReadOnly() ) {
-                               $this->user->saveSettings();
+                               // Promise that the token set here will be valid; save it at end of request
+                               $user = $this->user;
+                               \DeferredUpdates::addCallableUpdate( function () use ( $user ) {
+                                       $user->saveSettings();
+                               } );
                        }
                        $this->metaDirty = true;
                }