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

Vitest 3 - Freeze/out of memory error with useFakeTimers after upgrade #7288

Open
6 tasks done
rChaoz opened this issue Jan 18, 2025 · 4 comments
Open
6 tasks done

Vitest 3 - Freeze/out of memory error with useFakeTimers after upgrade #7288

rChaoz opened this issue Jan 18, 2025 · 4 comments
Labels
p2-to-be-discussed Enhancement under consideration (priority)

Comments

@rChaoz
Copy link

rChaoz commented Jan 18, 2025

Describe the bug

I'm facing an out-of-memory error of some of my tests right after upgrading to Vitest 3.

Reproduction

This is a simple test that causes it:

import { vi, expect, test } from 'vitest'

vi.useFakeTimers({ now: Date.now() })

test("test", async () => {
    const response = new Response(JSON.stringify({ success: false, message: "forbidden" }), { status: 403 })
    await expect(response.json()).resolves.toMatchInlineSnapshot()
})

Stackblitz - instead of OOM, it will run forever/freeze instead. Locally it will run for 20-60s and crash with OOM, I guess depending on your PC/RAM/whatever

Stacktrace/output
Error: Worker exited unexpectedly
    at ChildProcess.<anonymous> (file:///C:/Users/Matei/Programming/Web%20projects/todo-boards/node_modules/tinypool/dist/index.js:139:34)
    at ChildProcess.emit (node:events:531:35)
    at ChildProcess._handle.onexit (node:internal/child_process:294:12)

<--- Last few GCs --->

[5432:000001AD7D4D0080]    48925 ms: Mark-Compact 4054.3 (4137.8) -> 4041.3 (4140.8) MB, 996.29 / 0.00 ms  (average mu = 0.458, current mu = 0.012) allocation failure; scavenge might not succeed
[5432:000001AD7D4D0080]    50660 ms: Mark-Compact 4057.6 (4141.3) -> 4044.8 (4144.3) MB, 1724.07 / 0.00 ms  (average mu = 0.233, current mu = 0.007) allocation failure; scavenge might not succeed


<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
----- Native stack trace -----

 1: 00007FF73629EEDB node::SetCppgcReference+18123
 2: 00007FF736210C34 DSA_meth_get_flags+89940
 3: 00007FF736C87A71 v8::Isolate::ReportExternalAllocationLimitReached+65
 4: 00007FF736C711C8 v8::Function::Experimental_IsNopFunction+1336
 5: 00007FF736AD2C70 v8::Platform::SystemClockTimeMillis+659328
 6: 00007FF736ACFCF8 v8::Platform::SystemClockTimeMillis+647176
 7: 00007FF736AE500A v8::Platform::SystemClockTimeMillis+733978
 8: 00007FF736AE5887 v8::Platform::SystemClockTimeMillis+736151
 9: 00007FF736AF417F v8::Platform::SystemClockTimeMillis+795791
10: 00007FF7367B4865 v8::CodeEvent::GetFunctionName+116773
11: 00007FF736D393FE v8::PropertyDescriptor::writable+678094
12: 00007FF6B6F95830 

Failure cause not provided for 'test'
System info - probably not relevant
System:
    OS: Windows 11 10.0.22631
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700KF
    Memory: 14.09 GB / 31.86 GB
  Binaries:
    Node: 20.15.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 4.6.0 - C:\Program Files\nodejs\yarn.CMD
    npm: 10.7.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @vitest/coverage-v8: 3.0.2 => 3.0.2 
    @vitest/ui: 3.0.2 => 3.0.2 
    vite: 5.4.11 => 5.4.11 
    vitest: ^3.0.2 => 3.0.2

Used Package Manager

yarn

Validations

@rChaoz rChaoz changed the title Vitest 3 - Out of memory error Vitest 3 - Freeze/uut of memory error after upgrade Jan 18, 2025
@rChaoz rChaoz changed the title Vitest 3 - Freeze/uut of memory error after upgrade Vitest 3 - Freeze/out of memory error after upgrade Jan 18, 2025
@hi-ogawa
Copy link
Contributor

hi-ogawa commented Jan 19, 2025

This is likely due to the breaking change of fake timer https://vitest.dev/guide/migration.html#fake-timers-defaults

undici (Node's fetch implementation) seems to use queueMicrotask internally and that one is mocked in Vitest 3 by default. You can configure toFake explicitly to avoid mocking queueMicrotask as explained in the doc.
https://stackblitz.com/edit/vitest-dev-vitest-bmskququ?file=test%2Fbasic.test.ts

(OOM is obviously bad in any case, but I'm not sure if there's anything Vitest can do.)

@rChaoz
Copy link
Author

rChaoz commented Jan 19, 2025

I understand that, but still I think something is fundamentally wrong here - the test has nothing to do with timers. I don't think responses not working should be the intended behaviour, if so there should be a big bold disclaimer in docs about this. Even then, it should tell me, not crash with OOM error that was very hard to pinpoint to this.

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Jan 19, 2025

For reference, "mock everything available by default" was also a general direction of the underlying library @sinonjs/fake-timers, but it looks like they also have some reports due to such change:

@rChaoz
Copy link
Author

rChaoz commented Jan 19, 2025

For anyone experiencing anything similar, I just tested and indeed adding the config for the fake timers fixes this:

fakeTimers: {
    toFake: ["Date", "setTimeout", "clearTimeout"],
},

@hi-ogawa hi-ogawa changed the title Vitest 3 - Freeze/out of memory error after upgrade Vitest 3 - Freeze/out of memory error with useFakeTimers after upgrade Jan 20, 2025
@sheremet-va sheremet-va marked this as a duplicate of #7314 Jan 20, 2025
sergei-maertens added a commit to open-formulieren/open-forms-sdk that referenced this issue Jan 22, 2025
undici (the node fetch implementation) is affected by this because
it uses queueMicrotask, leading to OOM / never completing tests
that use fake timers. See
vitest-dev/vitest#7288 for more
complete information.
sergei-maertens added a commit to open-formulieren/open-forms-sdk that referenced this issue Jan 22, 2025
undici (the node fetch implementation) is affected by this because
it uses queueMicrotask, leading to OOM / never completing tests
that use fake timers. See
vitest-dev/vitest#7288 for more
complete information.
@sheremet-va sheremet-va added p2-to-be-discussed Enhancement under consideration (priority) and removed pending triage labels Jan 22, 2025
@sheremet-va sheremet-va moved this to P2 - 3 in Team Board Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-to-be-discussed Enhancement under consideration (priority)
Projects
Status: P2 - 3
Development

No branches or pull requests

3 participants