Skip to content

fix: close the build watch watcher on SIGINT/SIGTERM - #24182

Closed
doge-th wants to merge 1 commit into
vitejs:mainfrom
doge-th:fix/build-watch-close-watcher-on-signals
Closed

doge-th wants to merge 1 commit into
vitejs:mainfrom
doge-th:fix/build-watch-close-watcher-on-signals

Conversation

@doge-th

@doge-th doge-th commented Nov 22, 2026

Copy link
Copy Markdown

Fixes #24181.

The bug

vite build --watch died from SIGINT/SIGTERM without closing its watchers, so plugin closeWatcher hooks never ran: a plugin that cleans up in closeWatcher (temp files, child processes) leaked on every interrupted watch build — TanStack Start left one temp directory per interrupted build (330 on one machine). rollup -w, rolldown -w and vite dev all close on both signals; setupSIGTERMListener is registered by the dev and preview servers only.

The fix

When the watch watcher is created in buildEnv, listen for SIGINT/SIGTERM: close the watcher (running closeWatcher hooks), then exit with the conventional code (130 for SIGINT, 143 for SIGTERM, matching commandLock.ts's convention).

Interaction with the command lock (registered earlier by the CLI's build action): its handlers release the lock and re-raise the signal; these process.on handlers consume the re-raised signal while the watcher closes (a closing guard prevents double invocation), then exit explicitly — relying on a default death there would cut the close short. Without the lock (--bypassLock) the same handlers run on the original signal.

Verified (built from source, real vite build --watch with the issue's log-hooks plugin)

closeWatcher prints exit code
before (main, SIGTERM) ❌ never 143
after (SIGTERM) ✅ 143
after (SIGINT) ✅ 130

Baseline rebuilt from stashed main reproduced the bug (no closeWatcher on SIGTERM); rebuilding with the fix shows it on both signals.

`vite build --watch` died from SIGINT/SIGTERM without closing its
watchers, so plugin `closeWatcher` hooks never ran: a plugin that
cleans up in `closeWatcher` (temp files, child processes) leaked on
every interrupted watch build (TanStack Start left 330 temp directories
on one machine). `rollup -w`, `rolldown -w` and `vite dev` close on
both signals.

When the watch watcher is created, listen for SIGINT/SIGTERM: close the
watcher (running `closeWatcher` hooks), then exit with the conventional
code (130/143). The command lock's handlers release the lock and
re-raise the signal; these consume the re-raised signal while the
watcher closes, guarded against double invocation.

Fixes vitejs#24181
@github-actions github-actions Bot added the bot: likely Likely a bot, LLM, or agent. Automatically comments and closes the issue or PR label Nov 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically flagged as likely to be created by a bot, LLM, or agent, and will be automatically closed. These contributions harm the maintenance of the project. Please read our AI policy for more information.

If you believe this is a mistake, please reply to this comment and we will review it.

@github-actions github-actions Bot closed this Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot: likely Likely a bot, LLM, or agent. Automatically comments and closes the issue or PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vite build --watch does not close its watchers on Ctrl-C or SIGTERM (closeWatcher never runs)

1 participant