From 03ffa422be14e676eb3bb662cecad183f48cb567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Wed, 28 Jun 2023 16:48:55 +0100 Subject: [PATCH] fix(core): fix race condition in task scheduler (#17837) --- .../nx/src/tasks-runner/tasks-schedule.ts | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/nx/src/tasks-runner/tasks-schedule.ts b/packages/nx/src/tasks-runner/tasks-schedule.ts index 1ac169d6d598a..27cfe550197ba 100644 --- a/packages/nx/src/tasks-runner/tasks-schedule.ts +++ b/packages/nx/src/tasks-runner/tasks-schedule.ts @@ -25,10 +25,9 @@ export class TasksSchedule { private reverseTaskDeps = calculateReverseDeps(this.taskGraph); private reverseProjectGraph = reverse(this.projectGraph); private scheduledBatches: Batch[] = []; - private scheduledTasks: string[] = []; - private completedTasks = new Set(); + private scheduleRequestsExecutionChain = Promise.resolve(); constructor( private readonly hasher: TaskHasher, @@ -40,14 +39,9 @@ export class TasksSchedule { ) {} public async scheduleNextTasks() { - if (process.env.NX_BATCH_MODE === 'true') { - await this.scheduleBatches(); - } - for (let root of this.notScheduledTaskGraph.roots) { - if (this.canBeScheduled(root)) { - await this.scheduleTask(root); - } - } + this.scheduleRequestsExecutionChain = + this.scheduleRequestsExecutionChain.then(() => this.scheduleTasks()); + await this.scheduleRequestsExecutionChain; } public hasTasks() { @@ -83,6 +77,17 @@ export class TasksSchedule { : null; } + private async scheduleTasks() { + if (process.env.NX_BATCH_MODE === 'true') { + await this.scheduleBatches(); + } + for (let root of this.notScheduledTaskGraph.roots) { + if (this.canBeScheduled(root)) { + await this.scheduleTask(root); + } + } + } + private async scheduleTask(taskId: string) { const task = this.taskGraph.tasks[taskId];