Skip to content

Forward console plugin enters an infinite loop #22407

@jesusgalhub

Description

@jesusgalhub

Describe the bug

I run my vite app in dev mode, close the vite server and then my app enters an infinite loop that crash the navigator tab.
This is caused by vite forward console plugin. Sending an error produces another error, as the transport can't be successfully made. Hence the infinite loop.

Technical details

setupForwardConsoleHandler registers window.addEventListener("unhandledrejection", ...) which calls sendError()transport.send(). When the WebSocket disconnects (e.g. server restart), transport.send() rejects with "send was called before connect" (thrown by normalizeModuleRunnerTransport). That unhandled rejection is immediately caught by the same unhandledrejection listener, which calls sendError() again, creating an infinite loop of errors that floods the browser console.

The root cause is in packages/vite/src/shared/forwardConsole.ts:

function sendError(type: 'error' | 'unhandled-rejection', error: any) {
  transport.send({  // ← rejects when disconnected, no .catch()
    type: 'custom',
    event: 'vite:forward-console',
    data: { type, data: { name: error?.name, message: error?.message, stack: error?.stack } },
  })
}

window.addEventListener('unhandledrejection', (event) => {
  sendError('unhandled-rejection', event.reason)  // ← catches its own rejection → infinite loop
})

Possible fix

Add a .catch(() => {}) to transport.send() inside sendError (and optionally sendLog) to silently drop forwarding failures when the transport is disconnected:

function sendError(type: 'error' | 'unhandled-rejection', error: any) {
  transport.send({
    type: 'custom',
    event: 'vite:forward-console',
    data: {
      type,
      data: {
        name: error?.name || 'Unknown Error',
        message: error?.message || String(error),
        stack: error?.stack,
      },
    },
  }).catch(() => {
    // Silently ignore send failures (e.g. WebSocket disconnected).
    // Throwing here would re-trigger the unhandledrejection listener
    // and cause an infinite loop.
  })
}

Reproduction

https://stackblitz.com/edit/vitejs-vite-vuqsmzfx

Steps to reproduce

  1. npm install && npm run dev
  2. Open the browser, open DevTools console
  3. Close vite server
  4. Any error that fires during the disconnection window triggers the infinite loop. Click the button
  5. The browser console floods with repeated:
    Uncaught (in promise) Error: send was called before connect
        at Object.send (client:384:15)
        at sendError (client:480:13)
        at client:519:4
    
Image

System Info

System:
    OS: macOS 26.2
    CPU: (10) arm64 Apple M5
    Memory: 111.39 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.11.1 - /Users/jgonzalez.alvarez/.asdf/installs/ivm-node/24.11.1/bin/node
    npm: 11.6.2 - /Users/jgonzalez.alvarez/.asdf/installs/ivm-node/24.11.1/bin/npm
    pnpm: 10.33.0 - /Users/jgonzalez.alvarez/Library/pnpm/pnpm
    bun: 1.3.10 - /Users/jgonzalez.alvarez/.bun/bin/bun
  Browsers:
    Chrome: 147.0.7727.139
    Firefox: 147.0.4
    Safari: 26.2

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: devdev serverp3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions