From b0871c19e164a0222af4ca2c98a832aae08420ca Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sat, 2 Aug 2025 17:48:01 +1000 Subject: [PATCH] [v3] Fix: Vite server not cleaned up when build fails (#4436) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: properly clean up Vite server when build fails Ensures the Vite server process is terminated when a build fails during 'wails3 dev', preventing port conflicts on subsequent runs. Fixes #4403 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude * fix: add defer for watcher cleanup and tidy up returns - Add defer cleanup() to ensure resources are always freed - Remove manual cleanup in error path since defer handles it - Simplify error handling for better maintainability Addresses feedback on PR #4436 * fix: prevent channel deadlock in watcher cleanup - Separate cleanup logic from channel notification - cleanup() only stops the engine - signalCleanup() handles both cleanup and channel notification - Prevents deadlock when function exits normally --------- Co-authored-by: Claude --- v3/UNRELEASED_CHANGELOG.md | 1 + v3/internal/commands/watcher.go | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/v3/UNRELEASED_CHANGELOG.md b/v3/UNRELEASED_CHANGELOG.md index b680790f1..faa3653bf 100644 --- a/v3/UNRELEASED_CHANGELOG.md +++ b/v3/UNRELEASED_CHANGELOG.md @@ -24,6 +24,7 @@ After processing, the content will be moved to the main changelog and this file ## Fixed +- Fix Vite server not being cleaned up when build fails (#4403) ## Deprecated diff --git a/v3/internal/commands/watcher.go b/v3/internal/commands/watcher.go index 6cdd6dc3a..13f83ad61 100644 --- a/v3/internal/commands/watcher.go +++ b/v3/internal/commands/watcher.go @@ -35,17 +35,31 @@ func Watcher(options *WatcherOptions) error { if err != nil { return err } - signalHandler := signal.NewSignalHandler(func() { + + // Setup cleanup function that stops the engine + cleanup := func() { + watcherEngine.Stop() + } + defer cleanup() + + // Signal handler needs to notify when to stop + signalCleanup := func() { + cleanup() stopChan <- struct{}{} - }) + } + + signalHandler := signal.NewSignalHandler(signalCleanup) signalHandler.ExitMessage = func(sig os.Signal) string { return "" } signalHandler.Start() + + // Start the engine err = watcherEngine.Start() if err != nil { return err } + <-stopChan return nil }