Skip to content

fix: prevent rpc timeout on slow thread blocking synchronous methods #8297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 22, 2025

Conversation

AriPerkkio
Copy link
Member

@AriPerkkio AriPerkkio commented Jul 13, 2025

Description

Ref:

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Copy link

netlify bot commented Jul 13, 2025

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit bbb7b9a
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/687f936c578ee500084e97d4
😎 Deploy Preview https://deploy-preview-8297--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@AriPerkkio
Copy link
Member Author

AriPerkkio commented Jul 13, 2025

Worker side error can be tested locally with following changes:

diff --git a/packages/vitest/src/node/pools/rpc.ts b/packages/vitest/src/node/pools/rpc.ts
index 351043ad2..7d69b3bb3 100644
--- a/packages/vitest/src/node/pools/rpc.ts
+++ b/packages/vitest/src/node/pools/rpc.ts
@@ -92,6 +92,9 @@ export function createMethodsRPC(project: TestProject, options: MethodsOptions =
     async onTaskAnnotate(testId, annotation) {
       return ctx._testRun.annotate(testId, annotation)
     },
+    async exampleMethodThatsStuck() {
+      await new Promise((resolve) => {})
+    },
     async onTaskUpdate(packs, events) {
       if (options.collect) {
         ctx.state.updateTasks(packs)
diff --git a/packages/vitest/src/runtime/runners/index.ts b/packages/vitest/src/runtime/runners/index.ts
index 26d91a2a2..d5ef6f1c9 100644
--- a/packages/vitest/src/runtime/runners/index.ts
+++ b/packages/vitest/src/runtime/runners/index.ts
@@ -101,6 +101,7 @@ export async function resolveTestRunner(
   testRunner.onAfterRunFiles = async (files) => {
     const state = getWorkerState()
     const coverage = await takeCoverageInsideWorker(config.coverage, executor)
+    rpc().exampleMethodThatsStuck()
 
     if (coverage) {
       rpc().onAfterSuiteRun({
diff --git a/packages/vitest/src/types/rpc.ts b/packages/vitest/src/types/rpc.ts
index f6fd773e8..dc5447322 100644
--- a/packages/vitest/src/types/rpc.ts
+++ b/packages/vitest/src/types/rpc.ts
@@ -32,6 +32,8 @@ export interface RuntimeRPC {
   onCancel: (reason: CancelReason) => void
   getCountOfFailedTests: () => number
 
+  exampleMethodThatsStuck: () => void
+
   snapshotSaved: (snapshot: SnapshotResult) => void
   resolveSnapshotPath: (testPath: string) => string
 }
 RUN  v4.0.0-beta.2 /x/examples/packages/vitest-example

 ✓ test/add.test.ts (1 test) 1ms
   ✓ example 0ms
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: [vitest-worker]: Closing rpc while "exampleMethodThatsStuck" was pending
 ❯ ../../../vitest/packages/vitest/src/runtime/worker.ts:79:16
 ❯ ../../../birpc/dist/index.mjs:97:27
 ❯ Proxy.rejectPendingCalls ../../../birpc/dist/index.mjs:96:19
 ❯ ../../../vitest/packages/vitest/src/runtime/worker.ts:78:29
 ❯ ../../../vitest/packages/vitest/src/runtime/worker.ts:120:42
 ❯ execute ../../../vitest/packages/vitest/src/runtime/worker.ts:120:32
 ❯ onMessage ../../../vitest/node_modules/.pnpm/tinypool@1.1.1/node_modules/tinypool/dist/entry/process.js:39:18

This error originated in "test/add.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
This error was caught after test environment was torn down. Make sure to cancel any running tasks before test finishes:
- cancel timeouts using clearTimeout and clearInterval
- wait for promises to resolve using the await keyword
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯


 Test Files  1 passed (1)
      Tests  1 passed (1)
     Errors  1 error
   Start at  09:55:52
   Duration  200ms (transform 15ms, setup 0ms, collect 15ms, tests 1ms, environment 0ms, prepare 36ms)

@AriPerkkio AriPerkkio force-pushed the fix/rpc-timeout-ignore branch from d232fee to 935a6ea Compare July 14, 2025 17:21
@AriPerkkio AriPerkkio marked this pull request as ready for review July 14, 2025 17:31
@sheremet-va sheremet-va merged commit bea8746 into vitest-dev:main Jul 22, 2025
21 of 24 checks passed
@AriPerkkio AriPerkkio deleted the fix/rpc-timeout-ignore branch July 22, 2025 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

testTimeout is not fully respected: Error: [vitest-worker]: Timeout calling "onTaskUpdate"
2 participants