Skip to content

Commit

Permalink
Fix swallowed err from gcs close calls (#4706)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefferai authored Jun 5, 2018
1 parent 4e71de3 commit 2de2a30
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions physical/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/hashicorp/errwrap"
log "github.com/hashicorp/go-hclog"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/vault/helper/useragent"
"github.com/hashicorp/vault/physical"

Expand Down Expand Up @@ -166,7 +167,7 @@ func NewBackend(c map[string]string, logger log.Logger) (physical.Backend, error
}

// Put is used to insert or update an entry
func (b *Backend) Put(ctx context.Context, entry *physical.Entry) error {
func (b *Backend) Put(ctx context.Context, entry *physical.Entry) (retErr error) {
defer metrics.MeasureSince(metricPut, time.Now())

// Pooling
Expand All @@ -176,7 +177,12 @@ func (b *Backend) Put(ctx context.Context, entry *physical.Entry) error {
// Insert
w := b.client.Bucket(b.bucket).Object(entry.Key).NewWriter(ctx)
w.ChunkSize = b.chunkSize
defer w.Close()
defer func() {
closeErr := w.Close()
if closeErr != nil {
retErr = multierror.Append(retErr, errwrap.Wrapf("error closing connection: {{err}}", closeErr))
}
}()

if _, err := w.Write(entry.Value); err != nil {
return errwrap.Wrapf("failed to put data: {{err}}", err)
Expand All @@ -185,7 +191,7 @@ func (b *Backend) Put(ctx context.Context, entry *physical.Entry) error {
}

// Get fetches an entry. If no entry exists, this function returns nil.
func (b *Backend) Get(ctx context.Context, key string) (*physical.Entry, error) {
func (b *Backend) Get(ctx context.Context, key string) (retEntry *physical.Entry, retErr error) {
defer metrics.MeasureSince(metricGet, time.Now())

// Pooling
Expand All @@ -200,7 +206,13 @@ func (b *Backend) Get(ctx context.Context, key string) (*physical.Entry, error)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("failed to read value for %q: {{err}}", key), err)
}
defer r.Close()

defer func() {
closeErr := r.Close()
if closeErr != nil {
retErr = multierror.Append(retErr, errwrap.Wrapf("error closing connection: {{err}}", closeErr))
}
}()

value, err := ioutil.ReadAll(r)
if err != nil {
Expand Down

0 comments on commit 2de2a30

Please sign in to comment.