vite: Vite crash when switching between two significantly different branches - RangeError: Set maximum size exceeded

Describe the bug

In our case, by running vite in local dev and switching between our release branches with git checkout then we see the shell/vite crash and the browser die. It seems like HMR is not able to keep up with so many files changing so quickly.

Examination of the stack trace

It seems like since so many files change at the same time, it triggers this event handler many times: https://github.com/vitejs/vite/blob/8c87af7c03f5b989c8c86cf0c4efb0313c24af82/packages/vite/src/node/server/index.ts#L506-L511

The event handler is calling handleFileAddUnlink: https://github.com/vitejs/vite/blob/167753d3754507430600a1bc2b100ca321b17a86/packages/vite/src/node/server/hmr.ts#L195

Which subsequently calls updateModules: https://github.com/vitejs/vite/blob/167753d3754507430600a1bc2b100ca321b17a86/packages/vite/src/node/server/hmr.ts#L204

Calls propagateUpdate at this line over-and-over again (10+ times according to stack trace): https://github.com/vitejs/vite/blob/167753d3754507430600a1bc2b100ca321b17a86/packages/vite/src/node/server/hmr.ts#L313

Then calls propagateUpdate one more time before hitting the RangeError: https://github.com/vitejs/vite/blob/167753d3754507430600a1bc2b100ca321b17a86/packages/vite/src/node/server/hmr.ts#L148

Finally, the exception/crash is triggered at this point: https://github.com/vitejs/vite/blob/167753d3754507430600a1bc2b100ca321b17a86/packages/vite/src/node/server/hmr.ts#L246

Reproduction

not yet…

Steps to reproduce

  1. Checkout branch “A”
  2. Run yarn vite
  3. Checkout branch “B” that is significantly different than “A”
  4. Watch in the terminal and browser for the crash

System Info

Vite 4.1.1

Used Package Manager

yarn

Logs

boundaries.add({                                                                                                                                                                      
^

RangeError: Set maximum size exceeded
at Set.add (<anonymous>)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38291:20)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
at updateModules (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38231:28)
at handleFileAddUnlink (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38271:9)
at FSWatcher.<anonymous> (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:61281:9)
 at FSWatcher.emit (node:events:525:35)
at FSWatcher.emitWithAll (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:50325:8)
at FSWatcher._emit (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:50417:8)
at FSWatcher._remove (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:50680:50)
at FsEventsHandler.handleEvent (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:49529:16)
at FsEventsHandler.checkExists (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:49517:12)

I think the browser crash is possibly due to how HMR tries to do a full reload of the page but because the vite server crashed it shows Chrome as unavailable: image

Possibly related stack trace that doesn’t cause total crash

The stack trace below can happen but in an overlay and appears to be more recoverable.

When a lot of files change but not as many as when switching between our release branches, then it doesn’t lead to a complete vite crash but it has this stack trace that displays in the vite overlay. I suspect that the stack trace below and the one above have a related cause (i.e., lots of files change):

Set maximum size exceeded
    at Set.add (<anonymous>)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38291:20)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at propagateUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38347:13)
    at updateModules (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38231:28)
    at handleHMRUpdate (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:38219:5)
    at FSWatcher.<anonymous> (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:61267:23)
    at FSWatcher.emit (node:events:525:35)
    at FSWatcher.emitWithAll (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:50325:8)
    at FSWatcher._emit (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:50417:8)
    at FsEventsHandler.handleEvent (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:49551:14)
    at FsEventsHandler.addOrChange (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js:49501:8)
    at FsEventsHandler.checkExists (file:///xxx/node_modules/vite/dist/node/chunks/dep-3007b26d.js

Validations

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 29 (28 by maintainers)

Commits related to this issue

Most upvoted comments

You may be on to something @fc there’s the block of code above that is supposed to return early in the event of the circular dependency:

https://github.com/vitejs/vite/blob/167753d3754507430600a1bc2b100ca321b17a86/packages/vite/src/node/server/hmr.ts#L308-L311

But again, like for sets, this won’t work if the module node is not the same reference.

Maybe it needs to be:

 if (currentChain.some((node: ModuleNode) => node.id === importer.id)) { 
   // circular deps is considered dead end 
   return true 
 } 

Thanks. The propagateUpdates function is very quick on the server either way. It’s the update list containing thousands of duplicates that makes the client slow. So the client plugin assumes that every item in the list needs to be updated.

But fixing propagateUpdates recrawling nodes should probably fix both the HMR issue and the issue on this ticket of overflowing the call stack with a bunch of changes. If that is indeed the case

Hi @fc I made a pr #12125 that uses a hash to compare duplicated modules. Let me know if that fixes it

You the man! I’m on the road a bit and I think my local dev is messed up so will try again later today/tomorrow.