Skip to content

Commit

Permalink
Avoid closing a writer more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalbe4 committed Dec 15, 2023
1 parent 287c9b6 commit dcc45e7
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions artifactory/services/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,30 @@ func (ds *DownloadService) DownloadFiles(downloadParams ...DownloadParams) (oper
errorsQueue := clientutils.NewErrorsQueue(1)
expectedChan := make(chan int, 1)
successCounters := make([]int, ds.GetThreads())

// These two flags are used to ensure we do not attempt to close
// the writers more than once.
filesTransfersWriterClosed := false
artifactsDetailsWriterClosed := false

if ds.saveSummary {
ds.filesTransfersWriter, err = content.NewContentWriter(content.DefaultKey, true, false)
if err != nil {
return nil, err
}
defer func() {
err = errors.Join(err, ds.filesTransfersWriter.Close())
if !filesTransfersWriterClosed {
err = errors.Join(err, ds.filesTransfersWriter.Close())
}
}()
ds.artifactsDetailsWriter, err = content.NewContentWriter(content.DefaultKey, true, false)
if err != nil {
return nil, err
}
defer func() {
err = errors.Join(err, ds.artifactsDetailsWriter.Close())
if !artifactsDetailsWriterClosed {
err = errors.Join(err, ds.artifactsDetailsWriter.Close())
}
}()
}
ds.prepareTasks(producerConsumer, expectedChan, successCounters, errorsQueue, downloadParams...)
Expand All @@ -117,6 +127,21 @@ func (ds *DownloadService) DownloadFiles(downloadParams ...DownloadParams) (oper
for _, v := range successCounters {
totalSuccess += v
}

// It is important to close the two writers now, before proceeding
// to the operational summary generation. This is due to the fact that the
// operational summary generation flow reads the source files of the writers.
if err = ds.filesTransfersWriter.Close(); err != nil {
return
}
// This is done to avoid attempting to close the writer again inside the defer clause.
filesTransfersWriterClosed = true
if err = ds.artifactsDetailsWriter.Close(); err != nil {
return
}
// This is done to avoid attempting to close the writer again inside the defer clause.
artifactsDetailsWriterClosed = true

opertaionSummary = ds.getOperationSummary(totalSuccess, <-expectedChan-totalSuccess)
return
}
Expand Down

0 comments on commit dcc45e7

Please sign in to comment.