Skip to content

Vite dev: runtime close hook is skipped on Ctrl+C and server.close() #4586

Closed

Description

@cprecioso

Environment

  • Nitro: 3.0.260610-beta
  • Vite: 8.2.2
  • Node.js: v24.20.0
  • macOS: 26.6.2, arm64
  • Official nitrojs/starter Vite template, using nitro/vite with the default development runner. No Nuxt or other framework.
  • Also inspected Nitro main at c5177e9218cdd113c6c6a6a74b2924b2354af120; the Vite worker's shutdown handler is still empty there. The runtime reproduction below was executed with the published beta listed above.

Reproduction

Ready-to-run repository: https://github.com/cprecioso/nitro-vite-close-repro

Based on the official Nitro Vite starter, with a runtime lifecycle plugin, pinned dependencies, and automated checks added.

git clone https://github.com/cprecioso/nitro-vite-close-repro.git
cd nitro-vite-close-repro
npm ci
npm run repro

The check initializes the plugin and makes successful requests to the starter API before testing two independent shutdown paths:

  1. Sending SIGINT to the Vite CLI process (equivalent to Ctrl+C).
  2. Explicitly awaiting Vite's server.close() API.

Expected: each shutdown invokes the runtime plugin's close hook once.

Actual: both checks record only initialization. npm run repro exits with code 1. The hook writes synchronously to a file, so this does not depend on async cleanup finishing or console output being flushed. The API check also waits 500 ms after server.close() resolves before checking. Child processes are cleaned up after the assertions.

For a manual check, run npm run dev, visit /api/hello, press Ctrl+C, and inspect lifecycle.log. It contains init, but no close.

Describe the bug

Runtime plugin cleanup is skipped when stopping the Vite development server. The same omission occurs when starting Vite through createServer(), making a successful request, and explicitly awaiting server.close(): that promise resolves without the Nitro runtime close callback running.

This prevents plugins from relying on the documented runtime hook to release resources during Vite development shutdown.

Additional context

Logs

FAIL SIGINT: expected ["init","close"], got ["init"]
FAIL server.close(): expected ["init","close"], got ["init"]

Activity

  1. benpsnyder commented on Sep 7, 2026

    @benpsnyder

    Confirmed a related build-time lifecycle omission with Nitro 3.0.260903-beta on Node 24.15.0: awaiting Vite server.close() does not invoke the Nitro configuration hooks.close callback. This is separate from the runtime hook reported above.

    Minimal check inside a project with vite and nitro installed:

    import { createServer } from 'vite';
    import { nitro } from 'nitro/vite';
    
    let closed = false;
    const server = await createServer({
      configFile: false,
      plugins: [nitro({ hooks: { close() { closed = true; } } })],
    });
    await server.close();
    console.log({ closed }); // false; expected true

    In an isolated fixture, explicitly closing the retained Nitro instance invokes the callback. analogjs/analog#2535 passes all Windows HTTP/rendering assertions but retains filesystem resources after shutdown. This lifecycle omission is a candidate cause; the Windows causal link is not yet verified.

    Use case: programmatic Vite servers in integration tests need shutdown to release Nitro watchers and resources.

    Suggested AI prompt: “Reproduce the missing build-time Nitro close hook with programmatic Vite startup/shutdown. Trace ownership separately from runtime worker hooks, implement awaited cleanup exactly once, and test restart plus Windows process exit without forcing a successful exit.”

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