resourceloader: Jobs created in request() should wait for executing modules
authorTimo Tijhof <krinklemail@gmail.com>
Fri, 11 Sep 2015 05:20:54 +0000 (06:20 +0100)
committerKrinkle <krinklemail@gmail.com>
Fri, 11 Sep 2015 06:27:34 +0000 (06:27 +0000)
Fix a regression from 6e903b94f2. It failed to update this code to account for
the new "executing" state.

This code was sometimes causing the job dependencies to be an empty array
when the module in question is currently being executed. As such the job
wasn't blocked by anything and happily run the next time a random module
invoked handlePending to run any jobs that have been satisfied.

Scenario:

* in-page RLQ: Page loads a module X that contains scripts and styles.
  - state=loading
* Module X is found in storage and implemented.
  - state=executing
* execute() adds styles to css buffer (which is asynchronous).
  It provides a callback from which it would run the module's script, set
  to "ready", and trigger handlePending().
* in-page RLQ: The edit page uses 'mw.loader.using' to require module X
  and has a callback that uses the module.
* using() sees X is not already "ready", so it calls request() for X.
* request() creates a job for "X". It then wrongly filters out X from the list
  of modules to wait for. The filter is intended to remove modules that are
  complete (e.g. "ready", "error", or "missing"). After 6e903b94f2, it also
  removed anything with state "executing". This resulted in a job with
  an empty dependency list (nothing to wait for).
* The callstack is finished. At some point the css buffer will call back.
  Some other module requested before X enters state "ready" and triggers
  handlePending(). It finds an left-over empty job with no dependencies
  and runs it. "It" being the callback of X â€“ eventhough X isn't ready.

The added test fails without the change in mediawiki.js.

Bug: T112232
Change-Id: I3cc0c282e68a37b9b3256b213508362734161655

resources/src/mediawiki/mediawiki.js
tests/qunit/suites/resources/mediawiki/mediawiki.test.js

index c6e41c4..eb21c57 100644 (file)
                                // Add ready and error callbacks if they were given
                                if ( ready !== undefined || error !== undefined ) {
                                        jobs.push( {
+                                               // Narrow down the list to modules that are worth waiting for
                                                dependencies: $.grep( dependencies, function ( module ) {
                                                        var state = mw.loader.getState( module );
-                                                       return state === 'registered' || state === 'loaded' || state === 'loading';
+                                                       return state === 'registered' || state === 'loaded' || state === 'loading' || state === 'executing';
                                                } ),
                                                ready: ready,
                                                error: error
index 86c2eb8..b55b1e7 100644 (file)
                mw.loader.load( target );
        } );
 
+       QUnit.asyncTest( 'mw.loader() executing race (T112232)', 2, function ( assert ) {
+               var done = false;
+
+               // The red herring schedules its CSS buffer first. In T112232, a bug in the
+               // state machine would cause the job for testRaceLoadMe to run with an earlier job.
+               mw.loader.implement(
+                       'testRaceRedHerring',
+                       function () {},
+                       { css: [ '.mw-testRaceRedHerring {}' ] }
+               );
+               mw.loader.implement(
+                       'testRaceLoadMe',
+                       function () {
+                               done = true;
+                       },
+                       { css: [ '.mw-testRaceLoadMe { float: left; }' ] }
+               );
+
+               mw.loader.load( [ 'testRaceRedHerring', 'testRaceLoadMe' ] );
+               mw.loader.using( 'testRaceLoadMe', function () {
+                       assert.strictEqual( done, true, 'script ran' );
+                       assert.strictEqual( mw.loader.getState( 'testRaceLoadMe' ), 'ready', 'state' );
+               } ).always( QUnit.start );
+       } );
+
        QUnit.test( 'mw.html', 13, function ( assert ) {
                assert.throws( function () {
                        mw.html.escape();