Skip to content

Worker process is never killed when Vitest fails to start, so vitest-vscode processes accumulate #841

Backlog

Description

@DANIELK121

Describe the bug

When a config fails to start (Vitest failed to start: …), the extension rejects the startup but never kills the spawned worker process or closes its WebSocket server. Every later config reload spawns a new worker for the same config, which fails and also stays alive.

Over a few days of switching git branches I ended up with 49 idle vitest-vscode processes (~100 MB each), all for the same failing config. The extension log showed exactly 49 Vitest failed to start errors for that config.

Reproduction

I observed it with a config that throws during load (a required env var is missing) and with a custom reporter that fails to load (Failed to load custom Reporter from …). I haven't verified the minimal steps below in isolation, but they should hit the same path:

  1. Add a config that throws during load:
    // vitest.config.ts
    import { defineConfig } from 'vitest/config'
    if (!process.env.REQUIRED_VAR) throw new Error('REQUIRED_VAR not set')
    export default defineConfig({})
  2. Open the workspace. The output shows Vitest failed to start.
  3. Trigger a reload by editing any vitest.config.* (or switching git branches).
  4. pgrep -f vitest-vscode | wc -l grows by one per reload; the processes never exit.

Cause

In createVitestProcess (packages/extension/src/spawn/child_process.ts), the child is only wrapped in ExtensionChildProcess, the class that kills it when the WebSocket closes, on the success path:

waitForWsConnection(wss, pkg, 'child_process', options)
  .then((meta) => {
    const process = new ExtensionChildProcess(vitest, server, meta.ws)
    resolve({ ...meta, process })
  }, reject) // failure: only rejects; `vitest` (child) and `server` stay alive

On a type: 'error' message, onWsConnection (ws.ts) rejects and removes its listeners without closing the socket. The caller only receives the error, so nothing can kill the process, and the worker stays connected forever.

Suggested fix: in the rejection path, kill the child and close the server.

Small related bug in the same finally: vitest.off('exit', onError) should be vitest.off('error', onError).

Environment

  • vitest.explorer 1.52.2 (same code on main), running in Cursor (VS Code fork)
  • Vitest 5.1.0 / 5.1.1, Node 24.15.0, macOS 15

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions