Skip to content

Commit

Permalink
gcp/saver: Cancel the context on io.Copy failure during upload
Browse files Browse the repository at this point in the history
This is the proper way to make sure we do not write a partial file.

Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
  • Loading branch information
dwbuiten committed May 2, 2024
1 parent 15e0c83 commit 4b908fd
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/storage/gcp/saver.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,12 @@ func (s *Storage) upload(ctx context.Context, path string, stream io.Reader, fir
const op errors.Op = "gcp.upload"
ctx, span := observ.StartSpan(ctx, op.String())
defer span.End()
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()

wc := s.bucket.Object(path).If(storage.Conditions{
DoesNotExist: true,
}).NewWriter(ctx)
}).NewWriter(cancelCtx)

// We set this metadata only for the first of the three files uploaded,
// for use as a singleflight lock.
Expand All @@ -147,7 +150,7 @@ func (s *Storage) upload(ctx context.Context, path string, stream io.Reader, fir
// Once we support private storage buckets this may need refactoring
// unless there is a way to set the default perms in the project.
if _, err := io.Copy(wc, stream); err != nil {
_ = wc.Close()
// Purposely do not close it to avoid creating a partial file.
return err
}

Expand Down

0 comments on commit 4b908fd

Please sign in to comment.