Skip to content

Conversation

@surc54
Copy link
Contributor

@surc54 surc54 commented Apr 15, 2024

Description

I've been running into an issue where Vitest would hang after the testing is completed with the following output:

close timed out after 10000ms
Tests closed successfully but something prevents the main process from exiting
You can try to identify the cause by enabling "hanging-process" reporter. See https://vitest.dev/config/#reporters

Luckily, this repo has a reproducible setup at test/cli/fixtures/project -- in this directory, running npx vitest run --project space_1 causes the error above (you may need to try multiple times, but I've been getting it consistently).

The only other report I've found is by this commenter #2008 (comment) (the issue itself seems to be about a different time out issue).

I've tracked this down to a Vite web-socket server not being cleaned up at the end of the Vitest session.

This PR edits the Vitest#close() method to close the Vite dev servers on ALL resolved projects instead of just the filtered projects.

I tried to write a new test under test/cli but it seems that the Vite servers for the fixture projects do not get launched due to a port conflict with the Vitest instance running the actual test. I could not find a way to use a different port.

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.
  • (N/A) 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

  • (N/A) 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:.

@netlify
Copy link

netlify bot commented Apr 15, 2024

Deploy Preview for fastidious-cascaron-4ded94 canceled.

Name Link
🔨 Latest commit 4c840e5
🔍 Latest deploy log https://app.netlify.com/sites/fastidious-cascaron-4ded94/deploys/661ef62a44c9a900088f4583

@AriPerkkio
Copy link
Member

Interesting finding! How did you narrow the root cause to the websocket connections? Looks like Vitest's hanging-process reporter is unable to show those connections - I wonder if we could improve it to include them as well.

$ npx vitest run --project space_1 --reporter hanging-process --reporter basic

 RUN  v1.5.0 /Users/x/vitest/test/cli/fixtures/project

 ✓ |space_1| base.test.ts  (1 test) 1ms

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  08:15:25
   Duration  147ms (transform 12ms, setup 0ms, collect 6ms, tests 1ms, environment 0ms, prepare 45ms)

There are 12 handle(s) keeping the process running

# Tinypool
node:internal/async_hooks:202                                                                                    
node:internal/async_hooks:504                                                                                    
file:///Users/x/vitest/node_modules/.pnpm/[email protected]/node_modules/tinypool/dist/esm/index.js:37 
file:///Users/x/vitest/node_modules/.pnpm/[email protected]/node_modules/tinypool/dist/esm/index.js:58 
file:///Users/x/vitest/node_modules/.pnpm/[email protected]/node_modules/tinypool/dist/esm/index.js:945
file:///Users/x/vitest/packages/vitest/dist/vendor/cac.WCfoZ7tE.js:8776                             
file:///Users/x/vitest/packages/vitest/dist/vendor/cac.WCfoZ7tE.js:9425                             
file:///Users/x/vitest/packages/vitest/dist/vendor/cac.WCfoZ7tE.js:9450                             

# FILEHANDLE
node:internal/async_hooks:202

... FILEHANDLE x9 here

# FILEHANDLE
node:internal/async_hooks:202
close timed out after 10000ms
Tests closed successfully but something prevents the main process from exiting
You can try to identify the cause by enabling "hanging-process" reporter. See https://vitest.dev/config/#reporters

It's only reporting FILEHANDLE's for this case. The # Tinypool \n node:internal/async_hooks:202 part is coming from here https://github.com/tinylibs/tinypool/blob/f86e82927371b54ba26577d818b86393ccf902c2/src/EventEmitterAsyncResource.ts#L24 - we should probably add emitDestroy for those in Tinypool's pool.destroy() so that they wouldn't show up in the report.

Copy link
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

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

Same logic should be used here:

const runningServers = [this.server, ...this.projects.map(p => p.server)].filter(Boolean).length
if (runningServers === 1)
console.warn('Tests closed successfully but something prevents Vite server from exiting')
else if (runningServers > 1)
console.warn(`Tests closed successfully but something prevents ${runningServers} Vite servers from exiting`)
else
console.warn('Tests closed successfully but something prevents the main process from exiting')
console.warn('You can try to identify the cause by enabling "hanging-process" reporter. See https://vitest.dev/config/#reporters')

Then the reproduction case would print following:

close timed out after 10000ms
Tests closed successfully but something prevents 2 Vite servers from exiting

@surc54 surc54 force-pushed the fix-workspaces-project-cleanup branch from 65452aa to 4c840e5 Compare April 16, 2024 22:05
@surc54
Copy link
Contributor Author

surc54 commented Apr 16, 2024

@AriPerkkio Thanks for the review! I've updated that location to use this.resolvedProjects.

As for how I tracked it down, I used a debugger to run process._getActiveHandles() during that timeout period, which gave back something like this:

[ WriteStream, ReadStream, WriteStream, Server ]

The server caught my interest, and upon checking its address, I noticed that it was on port 24678. Searching the vite repository led me to the dev server code.

@surc54 surc54 requested a review from AriPerkkio April 16, 2024 22:23
@sheremet-va sheremet-va merged commit 413ec5e into vitest-dev:main Apr 17, 2024
@surc54 surc54 deleted the fix-workspaces-project-cleanup branch April 17, 2024 13:26
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.

3 participants