metacache: Always close the s2 writer (#10836)

The s2 writer could be leaked if there was an error.

Make sure it is always closed.
master
Klaus Post 4 years ago committed by GitHub
parent 71753e21e0
commit d1e1205036
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 55
      cmd/metacache-stream.go
  2. 5
      cmd/metacache-stream_test.go

@ -23,7 +23,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"os"
"strings" "strings"
"sync" "sync"
@ -84,56 +83,20 @@ func newMetacacheWriter(out io.Writer, blockSize int) *metacacheWriter {
return err return err
} }
w.closer = func() error { w.closer = func() (err error) {
if w.streamErr != nil { defer func() {
return w.streamErr cerr := s2w.Close()
} if err == nil && cerr != nil {
if err := w.mw.WriteBool(false); err != nil { err = cerr
return err }
} }()
if err := w.mw.Flush(); err != nil {
return err
}
return s2w.Close()
}
return nil
}
return &w
}
func newMetacacheFile(file string) *metacacheWriter {
w := metacacheWriter{
mw: nil,
}
w.creator = func() error {
fw, err := os.Create(file)
if err != nil {
return err
}
s2w := s2.NewWriter(fw, s2.WriterBlockSize(1<<20))
w.mw = msgp.NewWriter(s2w)
w.creator = nil
if err := w.mw.WriteByte(metacacheStreamVersion); err != nil {
return err
}
w.closer = func() error {
if w.streamErr != nil { if w.streamErr != nil {
fw.Close()
return w.streamErr return w.streamErr
} }
// Indicate EOS if err = w.mw.WriteBool(false); err != nil {
if err := w.mw.WriteBool(false); err != nil {
return err
}
if err := w.mw.Flush(); err != nil {
fw.Close()
return err
}
if err := s2w.Close(); err != nil {
fw.Close()
return err return err
} }
return fw.Close() return w.mw.Flush()
} }
return nil return nil
} }

@ -47,11 +47,6 @@ func loadMetacacheSampleEntries(t testing.TB) metaCacheEntriesSorted {
if err != io.EOF { if err != io.EOF {
t.Fatal(err) t.Fatal(err)
} }
if false {
w := newMetacacheFile("testdata/metacache-new.s2")
w.write(entries.entries()...)
w.Close()
}
return entries return entries
} }

Loading…
Cancel
Save