Skip to content

Commit

Permalink
fixup! s3store: Use context to stop producing parts
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Sep 15, 2023
1 parent 32744e0 commit b7b875a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
5 changes: 4 additions & 1 deletion pkg/s3store/s3store.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,10 @@ func (upload *s3Upload) uploadParts(ctx context.Context, offset int64, src io.Re
partProducer, fileChan := newS3PartProducer(src, store.MaxBufferedParts, store.TemporaryDirectory, store.diskWriteDurationMetric)

producerCtx, cancelProducer := context.WithCancel(ctx)
defer cancelProducer()
defer func() {
cancelProducer()
partProducer.closeUnreadFiles()
}()
go partProducer.produce(producerCtx, optimalPartSize)

var wg sync.WaitGroup
Expand Down
16 changes: 10 additions & 6 deletions pkg/s3store/s3store_part_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ func newS3PartProducer(source io.Reader, backlog int64, tmpDir string, diskWrite
return partProducer, fileChan
}

// closeUnreadFiles should always be called by the consumer to ensure that the channels
// are properly closed and emptied.
func (spp *s3PartProducer) closeUnreadFiles() {
// If we return while there are still files in the channel, then
// we may leak file descriptors. Let's ensure that those are cleaned up.
for fileChunk := range spp.files {
fileChunk.closeReader()
}
}

func (spp *s3PartProducer) produce(ctx context.Context, partSize int64) {
outerloop:
for {
Expand All @@ -66,13 +76,7 @@ outerloop:
}
}

// First, close the channel.
close(spp.files)

// And then empty all entries to ensure that we close the file descriptors.
for fileChunk := range spp.files {
fileChunk.closeReader()
}
}

func (spp *s3PartProducer) nextPart(size int64) (fileChunk, bool, error) {
Expand Down

0 comments on commit b7b875a

Please sign in to comment.