Skip to content

Race condition in new pool implementation when using bail #8852

@nicojs

Description

@nicojs

Describe the bug

There is a race condition in the new worker pool implementation.

Reproduction

See repro.zip.

The relevant part is the run-vitest.ts file.

import { createVitest } from "vitest/node";
const vitest = await createVitest("test", {
  pool: "threads",
  coverage: { enabled: false },
  maxWorkers: 1,
  maxConcurrency: 1,
  watch: false,
  bail: 1,
  reporters: [],
});

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

function reportErrors() {
  if (vitest.state.errorsSet.size > 0) {
    const msg = [...vitest.state.errorsSet]
      .map((err) => err.message)
      .join("\n");
    console.error("Test run errored with:\n", msg);
  } else {
    console.log("No errors!");
  }
}

I would expect this program to run infinitely. What actually happens:

  • It stops after ~1 second
  • It logs: [vitest-pool]: Cannot run tasks while pool is cancelling

The race condition concerns bail. The canceling of the run doesn't finish before the run itself finishes (there is 1 failing test).

repro.zip

System Info

System:
    OS: macOS 26.0.1
    CPU: (14) arm64 Apple M4 Pro
    Memory: 989.08 MB / 48.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.18.0 - /Users/nicojs/.nvm/versions/node/v22.18.0/bin/node
    Yarn: 1.22.22 - /Users/nicojs/.nvm/versions/node/v22.18.0/bin/yarn
    npm: 11.5.2 - /Users/nicojs/.nvm/versions/node/v22.18.0/bin/npm
    pnpm: 10.19.0 - /Users/nicojs/Library/pnpm/pnpm
  Browsers:
    Chrome: 141.0.7390.123
    Edge: 141.0.3537.99
    Firefox: 144.0
    Safari: 26.0.1
  npmPackages:
    vitest: ^4.0.4 => 4.0.4

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions