[v3] Fix: Vite server not cleaned up when build fails (#4436)

* 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>
This commit is contained in:
Lea Anthony 2025-08-02 17:48:01 +10:00 committed by GitHub
commit b0871c19e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 2 deletions

View file

@ -24,6 +24,7 @@ After processing, the content will be moved to the main changelog and this file
## Fixed
<!-- Bug fixes -->
- Fix Vite server not being cleaned up when build fails (#4403)
## Deprecated
<!-- Soon-to-be removed features -->

View file

@ -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
}