From eec0726b52c190066bf5e717c7d2ca454d1ea768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=A8=E9=9B=AA?= Date: Tue, 18 Aug 2026 19:07:18 +0800 Subject: [PATCH] fix(mihomo): restore config when initial start fails --- backend/internal/mihomo/service.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/backend/internal/mihomo/service.go b/backend/internal/mihomo/service.go index 777b6c5..fed00df 100644 --- a/backend/internal/mihomo/service.go +++ b/backend/internal/mihomo/service.go @@ -30,7 +30,7 @@ func (s *Service) SaveConfig(content string) error { // ApplyConfig validates and activates a candidate configuration. Before the // live file is changed, the current file is copied to .bak. If -// validation or restart fails, the previous configuration is restored. +// validation or start/restart fails, the previous configuration is restored. func (s *Service) ApplyConfig(content string) error { if s == nil || s.cm == nil { return fmt.Errorf("mihomo service not initialized") } old, readErr := s.cm.ReadConfig() @@ -45,7 +45,17 @@ func (s *Service) ApplyConfig(content string) error { if readErr == nil { _ = s.cm.SaveConfig(old) } return fmt.Errorf("validate Mihomo configuration: %w", err) } - if !wasRunning { return s.pm.Start() } + if !wasRunning { + if err := s.pm.Start(); err != nil { + if readErr == nil { + if restoreErr := s.cm.SaveConfig(old); restoreErr != nil { + return fmt.Errorf("start Mihomo: %v; restore previous config: %w", err, restoreErr) + } + } + return fmt.Errorf("start Mihomo: %w", err) + } + return nil + } if err := s.pm.Restart(); err != nil { if readErr == nil { if restoreErr := s.cm.SaveConfig(old); restoreErr == nil { _ = s.pm.Restart() }