Skip to content
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions packages/vitest/src/node/pools/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Pool {
private activeTasks: ActiveTask[] = []
private sharedRunners: PoolRunner[] = []
private exitPromises: Promise<void>[] = []
private _isCancelling: boolean = false
private cancellingPromise: Promise<void> | null = null

constructor(private options: Options, private logger: Logger) {}

Expand All @@ -47,9 +47,9 @@ export class Pool {
}

async run(task: PoolTask, method: 'run' | 'collect'): Promise<void> {
// Prevent new tasks from being queued during cancellation
if (this._isCancelling) {
throw new Error('[vitest-pool]: Cannot run tasks while pool is cancelling')
// Wait for any ongoing cancellation to complete before accepting new tasks
if (this.cancellingPromise) {
await this.cancellingPromise
Comment on lines +50 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this change will start some previous run's tests too. I have no idea why this._isCancelling was required before - @sheremet-va I think that's something you added?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea why this._isCancelling was required before

The comment explains it:

Prevent new tasks from being queued during cancellation

Copy link
Member

@sheremet-va sheremet-va Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These kinds of checks exist to catch any unintended behaviours

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we just await here and then let it continue, doesn't it mean it's going to continue the run it was intended the cancel? Should it return instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that was the previous cancellation (because of bail). So it needs to wait for that to finish.

}

// Every runner related failure should make this promise reject so that it's picked by pool.
Expand Down Expand Up @@ -167,28 +167,30 @@ export class Pool {
}

async cancel(): Promise<void> {
// Set flag to prevent new tasks from being queued
this._isCancelling = true
Comment on lines -170 to -171
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pool should already have had all tests queued at this point. Or if there was another group, the ctx.isCancelling in node/pool.ts should have prevented them from being queued.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this was true, there wouldn't have been an issue #8852

// Create a promise to track cancellation completion
const cancelPromise = (async () => {
const pendingTasks = this.queue.splice(0)

const pendingTasks = this.queue.splice(0)
if (pendingTasks.length) {
const error = new Error('Cancelled')
pendingTasks.forEach(task => task.resolver.reject(error))
}

if (pendingTasks.length) {
const error = new Error('Cancelled')
pendingTasks.forEach(task => task.resolver.reject(error))
}
const activeTasks = this.activeTasks.splice(0)
await Promise.all(activeTasks.map(task => task.cancelTask()))

const activeTasks = this.activeTasks.splice(0)
await Promise.all(activeTasks.map(task => task.cancelTask()))
const sharedRunners = this.sharedRunners.splice(0)
await Promise.all(sharedRunners.map(runner => runner.stop()))

const sharedRunners = this.sharedRunners.splice(0)
await Promise.all(sharedRunners.map(runner => runner.stop()))
await Promise.all(this.exitPromises.splice(0))

await Promise.all(this.exitPromises.splice(0))
this.workerIds.forEach((_, id) => this.freeWorkerId(id))

this.workerIds.forEach((_, id) => this.freeWorkerId(id))
this.cancellingPromise = null
})()

// Reset flag after cancellation completes
this._isCancelling = false
this.cancellingPromise = cancelPromise
await cancelPromise
}

async close(): Promise<void> {
Expand Down
9 changes: 9 additions & 0 deletions test/cli/fixtures/bail-race/add.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from 'vitest'

test('adds two numbers', () => {
expect(2 + 3).toBe(5)
})

test('fails adding two numbers', () => {
expect(2 + 3).toBe(6)
})
44 changes: 44 additions & 0 deletions test/cli/test/bail-race.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { resolve } from 'pathe'
import { expect, test } from 'vitest'
import { createVitest } from 'vitest/node'

test('cancels previous run before starting new one', async () => {
const results: string[] = []

const vitest = await createVitest('test', {
root: resolve(import.meta.dirname, '../fixtures/bail-race'),
bail: 1,
maxWorkers: 1,
watch: false,
reporters: [{
onTestCaseResult(testCase) {
const result = testCase.result()

results.push(`${result.state}${result.errors ? `: ${result.errors?.[0].message}` : ''}`)
},
}],
})

let rounds = 0

while (vitest.state.errorsSet.size === 0) {
await vitest.start()

if (rounds >= 2) {
break
}

rounds++
}

expect(results).toMatchInlineSnapshot(`
[
"passed",
"failed: expected 5 to be 6 // Object.is equality",
"passed",
"failed: expected 5 to be 6 // Object.is equality",
"passed",
"failed: expected 5 to be 6 // Object.is equality",
]
`)
})
Loading