metacache: Always close the s2 writer (#10836)

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

Make sure it is always closed.
This commit is contained in:
Klaus Post 2020-11-05 07:30:14 -08:00 committed by GitHub
parent 71753e21e0
commit d1e1205036
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 51 deletions

View File

@ -23,7 +23,6 @@ import (
"errors"
"fmt"
"io"
"os"
"strings"
"sync"
@ -84,56 +83,20 @@ func newMetacacheWriter(out io.Writer, blockSize int) *metacacheWriter {
return err
}
w.closer = func() error {
w.closer = func() (err error) {
defer func() {
cerr := s2w.Close()
if err == nil && cerr != nil {
err = cerr
}
}()
if w.streamErr != nil {
return w.streamErr
}
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 {
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 {
fw.Close()
return w.streamErr
}
// Indicate EOS
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 fw.Close()
return w.mw.Flush()
}
return nil
}

View File

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