Skip to content
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

fix(runner): long synchronous tasks does not time out (fix #2920) #6944

Conversation

ferdodo
Copy link
Contributor

@ferdodo ferdodo commented Nov 21, 2024

Description

Fixes #2920

This PR solves Vitest ignoring timeouts on long synchronous tasks, that are blocking until the task and the timeout are settled simultaneously.

In this case we want to give priority to the timeout, because a test running longer than it's own timeout shall fail.

  • The result of the test is wrapped into a promise, and delayed to be processed in the same event loop cycle as the timeout.
  • I changed the order in the Promise.race, to give the timeout priority over the other promise.
  • I added a test for this case that takes 15ms, both changes are necessary to make the test pass.
  • Using nextTick from getSafeTimers() over the additional setTimeout I used did not solve the issue.
  • Outputing logs during tests displays slighly differently.

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 Nov 21, 2024

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit 11793e8
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/675701e974c5d9000817190e
😎 Deploy Preview https://deploy-preview-6944--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 site configuration.

@ferdodo ferdodo force-pushed the fix-long-task-with-synchronous-pieces-does-not-time-out branch 2 times, most recently from aedbc6c to 7bc3c2a Compare November 21, 2024 16:41
Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

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

Nice find! The change makes sense to me.

Comment on lines +54 to +56
Promise.resolve(fn(...args)).then((result) => {
return new Promise(resolve => setTimeout(resolve, 0, result))
}),
Copy link
Contributor

@hi-ogawa hi-ogawa Nov 22, 2024

Choose a reason for hiding this comment

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

It took a while to understand this but the idea is more or less like this? Executing fn in sync as usual, but holding off returning until setTimeout(.., 0) to flush other setTimeout callbacks. I think code is fine as is, but it's just my commentary.

      (async () => {
        const result = await fn(...args);
        await new Promise(resolve => setTimeout(resolve, 0))
        return result;
      })()

Oh, well it looks like this code actually breaks test.extend, so I'm not sure I understood this well 😅 Ah I forgot about args

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks a lot! Yes that's the same thing, I can try refactor with your code if it's best.

@ferdodo
Copy link
Contributor Author

ferdodo commented Nov 22, 2024

The Browser: webkit test was passing in one of my previous run:

https://github.com/vitest-dev/vitest/actions/runs/11955759654

Is it a flakiness issue ? It might just need a job re-run to pass.

[Edit]: Just re-pushed an identical commit to trigger the re-run. Test are passing now ✅

@ferdodo ferdodo force-pushed the fix-long-task-with-synchronous-pieces-does-not-time-out branch from 7bc3c2a to 3414830 Compare November 22, 2024 22:52
@sheremet-va sheremet-va merged commit 2fb585a into vitest-dev:main Dec 9, 2024
17 checks passed
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.

Long task with synchronous pieces does not time out
3 participants