From c87125d003cf2097e2de3f653baba4adfb0c19b4 Mon Sep 17 00:00:00 2001 From: Paul Payne Date: Thu, 15 Oct 2026 07:23:40 +0000 Subject: [PATCH] fix: Clean up .bak files after SafeApply to prevent dnsmasq loading stale configs dnsmasq loads every file in /etc/dnsmasq.d/ (skipping only .dpkg-*). SafeApply was leaving .bak backup files after successful applies, causing duplicate/conflicting DNS records. Also moves dnsmasq's validation temp file to /tmp so a crash can't leave a .validate file in the conf dir. --- internal/dnsmasq/config.go | 8 ++++++-- internal/haproxy/config.go | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/dnsmasq/config.go b/internal/dnsmasq/config.go index ea0b396..fa7bdb6 100644 --- a/internal/dnsmasq/config.go +++ b/internal/dnsmasq/config.go @@ -303,8 +303,8 @@ log-dhcp // SafeApply validates, backs up, writes, restarts, and verifies the dnsmasq config. // On restart or verification failure, rolls back to the previous config. func (g *Manager) SafeApply(content string) error { - // Validate via temp file - tmpFile := g.configPath + ".validate" + // Validate via temp file (use /tmp to avoid stray files in dnsmasq.d if process crashes) + tmpFile := "/tmp/wild-central-dnsmasq-validate.tmp" if err := os.WriteFile(tmpFile, []byte(content), 0644); err != nil { return fmt.Errorf("writing validation file: %w", err) } @@ -336,6 +336,10 @@ func (g *Manager) SafeApply(content string) error { return fmt.Errorf("health check failed (rolled back): %w", err) } + // Clean up backup — dnsmasq loads every file in its conf dir, + // so stale .bak files cause duplicate/conflicting records. + os.Remove(g.configPath + ".bak") + return nil } diff --git a/internal/haproxy/config.go b/internal/haproxy/config.go index e56e00c..2cceb9a 100644 --- a/internal/haproxy/config.go +++ b/internal/haproxy/config.go @@ -576,6 +576,8 @@ func (m *Manager) SafeApply(content string) error { return fmt.Errorf("health check failed (rolled back): %w", err) } + os.Remove(m.configPath + ".bak") + return nil }