15112: Remove tempfile only if still locked (not renamed).
authorTom Clegg <tclegg@veritasgenetics.com>
Wed, 24 Apr 2019 20:30:00 +0000 (16:30 -0400)
committerTom Clegg <tclegg@veritasgenetics.com>
Wed, 24 Apr 2019 20:30:00 +0000 (16:30 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tclegg@veritasgenetics.com>

services/keep-balance/balance.go

index 33f3762b47b89d2d19d9e68f38051407b4f6fa38..fc5812cde34ae25cf38d5d5dd5c88f6864cd78b2 100644 (file)
@@ -83,7 +83,13 @@ func (bal *Balancer) Run(config Config, runOptions RunOptions) (nextRunOptions R
                if err != nil {
                        return
                }
-               defer os.Remove(tmpfn)
+               defer func() {
+                       // Remove the tempfile only if we didn't get
+                       // as far as successfully renaming it.
+                       if lbFile != nil {
+                               os.Remove(tmpfn)
+                       }
+               }()
                bal.lostBlocks = lbFile
        } else {
                bal.lostBlocks = ioutil.Discard
@@ -141,10 +147,7 @@ func (bal *Balancer) Run(config Config, runOptions RunOptions) (nextRunOptions R
                if err != nil {
                        return
                }
-               err = lbFile.Close()
-               if err != nil {
-                       return
-               }
+               lbFile = nil
        }
        if runOptions.CommitPulls {
                err = bal.CommitPulls(&config.Client)