From 06d16b7b51038fe1794b17ca7bbd0eafeb83ec6c Mon Sep 17 00:00:00 2001 From: Leo Antunes Date: Sat, 25 Aug 2018 22:27:38 +0200 Subject: [PATCH] close tmpfile before removing to avoid fd leak --- etchosts.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/etchosts.go b/etchosts.go index 241b97b..9028277 100644 --- a/etchosts.go +++ b/etchosts.go @@ -32,12 +32,12 @@ func writeToEtcHosts(ipsToNames ipsToNamesMap, config ConfigSpec) error { } // remove tempfile; this might fail if we managed to move it, which is ok - defer func(path string) { - err := os.Remove(path) - if err != nil && !os.IsNotExist(err) { - log.Warnf("unexpected error trying to remove temp file %s: %s", path, err) + defer func(file *os.File) { + file.Close() + if err := os.Remove(file.Name()); err != nil && !os.IsNotExist(err) { + log.Warnf("unexpected error trying to remove temp file %s: %s", file.Name(), err) } - }(tmp.Name()) + }(tmp) // go through file and update existing entries/prune nonexistent entries managedLine := false