Skip to content

Commit

Permalink
Merge pull request #12915 from webpack/bugfix/multi-compiler-dependen…
Browse files Browse the repository at this point in the history
…cies

fix race condition in MultiCompiler queueing
  • Loading branch information
sokra authored Mar 17, 2021
2 parents c67a73a + 174eb3c commit 966738e
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 13 deletions.
67 changes: 54 additions & 13 deletions lib/MultiCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,18 @@ module.exports = class MultiCompiler {
* @returns {SetupResult[]} result of setup
*/
_runGraph(setup, run, callback) {
/** @typedef {{ compiler: Compiler, result: Stats, state: "blocked" | "queued" | "running" | "done", children: Node[], parents: Node[] }} Node */
/** @typedef {{ compiler: Compiler, result: Stats, state: "pending" | "blocked" | "queued" | "running" | "running-outdated" | "done", children: Node[], parents: Node[] }} Node */

// State transitions for nodes:
// -> blocked (initial)
// blocked -> queued [add to queue] (when all parents done)
// queued -> running [running++] (when processing the queue)
// running -> done [running--] (when compilation is done)
// done -> pending (when invalidated from file change)
// pending -> blocked (when invalidated from aggregated changes)
// done -> blocked (when invalidated, from parent invalidation)
// running -> running-outdated (when invalidated, either from change or parent invalidation)
// running-outdated -> blocked [running--] (when compilation is done)

/** @type {Node[]} */
const nodes = this.compilers.map(compiler => ({
Expand Down Expand Up @@ -376,26 +387,41 @@ module.exports = class MultiCompiler {
running--;
if (node.state === "running") {
node.state = "done";
}
for (const child of node.children) {
if (child.state !== "blocked") continue;
if (child.parents.every(p => p.state === "done")) {
child.state = "queued";
queue.enqueue(child);
for (const child of node.children) {
checkUnblocked(child);
}
} else if (node.state === "running-outdated") {
node.state = "blocked";
checkUnblocked(node);
}
process.nextTick(processQueue);
};
/**
* @param {Node} node node
* @returns {void}
*/
const nodeInvalid = node => {
if (node.state === "done" || node.state === "running") {
const nodeInvalidFromParent = node => {
if (node.state === "done") {
node.state = "blocked";
} else if (node.state === "running") {
node.state = "running-outdated";
}
for (const child of node.children) {
nodeInvalid(child);
nodeInvalidFromParent(child);
}
};
/**
* @param {Node} node node
* @returns {void}
*/
const nodeInvalid = node => {
if (node.state === "done") {
node.state = "pending";
} else if (node.state === "running") {
node.state = "running-outdated";
}
for (const child of node.children) {
nodeInvalidFromParent(child);
}
};
/**
Expand All @@ -404,23 +430,34 @@ module.exports = class MultiCompiler {
*/
const nodeChange = node => {
nodeInvalid(node);
if (node.state === "pending") {
node.state = "blocked";
}
checkUnblocked(node);
processQueue();
};
/**
* @param {Node} node node
* @returns {void}
*/
const checkUnblocked = node => {
if (
node.state === "blocked" &&
node.parents.every(p => p.state === "done")
) {
node.state = "queued";
queue.enqueue(node);
processQueue();
}
};

const setupResults = [];
nodes.forEach((node, i) => {
setupResults.push(
setup(
node.compiler,
i,
nodeDone.bind(null, node),
() => node.state === "blocked" || node.state === "queued",
() => node.state !== "running",
() => nodeChange(node),
() => nodeInvalid(node)
)
Expand All @@ -434,7 +471,11 @@ module.exports = class MultiCompiler {
node.state = "running";
run(node.compiler, nodeDone.bind(null, node));
}
if (!errored && running === 0) {
if (
!errored &&
running === 0 &&
nodes.every(node => node.state === "done")
) {
const stats = [];
for (const node of nodes) {
const result = node.result;
Expand Down
46 changes: 46 additions & 0 deletions test/MultiCompiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,50 @@ describe("MultiCompiler", function () {
}
});
});
it("shouldn't hang when invalidating during build", done => {
const compiler = webpack(
Object.assign([
{
name: "a",
mode: "development",
context: path.join(__dirname, "fixtures"),
entry: "./a.js"
},
{
name: "b",
mode: "development",
context: path.join(__dirname, "fixtures"),
entry: "./b.js",
dependencies: ["a"]
}
])
);
compiler.outputFileSystem = createFsFromVolume(new Volume());
const watchCallbacks = [];
const watchCallbacksUndelayed = [];
let firstRun = true;
compiler.watchFileSystem = {
watch(
files,
directories,
missing,
startTime,
options,
callback,
callbackUndelayed
) {
watchCallbacks.push(callback);
watchCallbacksUndelayed.push(callbackUndelayed);
if (firstRun && files.has(path.join(__dirname, "fixtures", "a.js"))) {
process.nextTick(() => {
callback(null, new Map(), new Map(), new Set(), new Set());
});
firstRun = false;
}
}
};
compiler.watch({}, (err, stats) => {
done(err);
});
});
});

0 comments on commit 966738e

Please sign in to comment.