diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d4555d1f5..adfbb3c1d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ FEATURES: IMPROVEMENTS: BUG FIXES: +* agent: Clean up temporary files during disk write errors when persisting services and checks. [GH-3207] ## 0.9.0 (July 20, 2017) diff --git a/agent/agent.go b/agent/agent.go index 8fc32dce27..01c9ce6a38 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -1485,15 +1485,25 @@ func writeFileAtomic(path string, contents []byte) error { return err } if _, err := fh.Write(contents); err != nil { + fh.Close() + os.Remove(tempPath) return err } if err := fh.Sync(); err != nil { + fh.Close() + os.Remove(tempPath) return err } if err := fh.Close(); err != nil { + fh.Close() + os.Remove(tempPath) return err } - return os.Rename(tempPath, path) + if err := os.Rename(tempPath, path); err != nil { + os.Remove(tempPath) + return err + } + return nil } // AddService is used to add a service entry. @@ -2072,6 +2082,12 @@ func (a *Agent) loadServices(conf *Config) error { continue } + // Skip all partially written temporary files + if strings.HasSuffix(fi.Name(), "tmp") { + a.logger.Printf("[WARN] Ignoring temporary service file %v", fi.Name()) + continue + } + // Open the file for reading file := filepath.Join(svcDir, fi.Name()) fh, err := os.Open(file)