Fix file closing

This commit is contained in:
Timmy Welch 2024-10-31 21:12:53 -05:00
parent 840e97cff0
commit 7ede0dee72
2 changed files with 13 additions and 5 deletions

View File

@ -538,10 +538,10 @@ func (s *Server) reader(workerID int, done func(i int)) {
panic(err) panic(err)
} }
i, format, err := image.Decode(bufio.NewReader(file)) i, format, err := image.Decode(bufio.NewReader(file))
file.Close()
if err != nil { if err != nil {
continue // skip this image continue // skip this image
} }
file.Close()
im := ch.Im{ im := ch.Im{
Im: i, Im: i,
@ -759,16 +759,21 @@ func downloadProcessor(opts Opts, imagePaths chan cv.Download, server Server) {
continue continue
} }
file, err := os.Open(path.Dest) file, err := os.OpenFile(path.Dest, os.O_RDWR|os.O_CREATE, 0666)
if err != nil { if err != nil {
panic(err) panic(err)
} }
i, format, err := image.Decode(bufio.NewReader(file)) i, format, err := image.Decode(bufio.NewReader(file))
if err != nil { if err != nil {
file.Close()
continue // skip this image continue // skip this image
} }
if opts.truncateHashedImages { if opts.truncateHashedImages {
file.Truncate(0) file.Seek(0, io.SeekStart)
err = file.Truncate(0)
if err != nil {
log.Printf("Failed to truncate %#v: %v", path.Dest, err)
}
} }
file.Close() file.Close()

View File

@ -272,6 +272,7 @@ func (c *CVDownloader) updateIssues() {
} }
resp, err, cancelDownloadCTX := Get(c.Context, URI.String()) resp, err, cancelDownloadCTX := Get(c.Context, URI.String())
if err != nil { if err != nil {
_ = resp.Body.Close()
cancelDownloadCTX() cancelDownloadCTX()
if retry(URI.String(), err) { if retry(URI.String(), err) {
continue continue
@ -281,11 +282,13 @@ func (c *CVDownloader) updateIssues() {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
cancelDownloadCTX() cancelDownloadCTX()
if retry(URI.String(), nil) { if retry(URI.String(), nil) {
_ = resp.Body.Close()
continue continue
} }
log.Println("Failed to download this page, we'll wait for an hour to see if it clears up") log.Println("Failed to download this page, we'll wait for an hour to see if it clears up")
select { select {
case <-c.Context.Done(): // allows us to return immediately even during a timeout case <-c.Context.Done(): // allows us to return immediately even during a timeout
_ = resp.Body.Close()
return return
case <-time.After(1 * time.Hour): case <-time.After(1 * time.Hour):
} }
@ -296,6 +299,8 @@ func (c *CVDownloader) updateIssues() {
} }
body := io.TeeReader(resp.Body, file) body := io.TeeReader(resp.Body, file)
err = json.NewDecoder(bufio.NewReader(body)).Decode(issue) err = json.NewDecoder(bufio.NewReader(body)).Decode(issue)
_ = resp.Body.Close()
_ = file.Close()
if err != nil || issue.Offset != offset { if err != nil || issue.Offset != offset {
os.Remove(filepath.Join(c.JSONPath, "cv-"+strconv.Itoa(offset)+".json")) os.Remove(filepath.Join(c.JSONPath, "cv-"+strconv.Itoa(offset)+".json"))
cancelDownloadCTX() cancelDownloadCTX()
@ -304,8 +309,6 @@ func (c *CVDownloader) updateIssues() {
} }
return return
} }
_ = resp.Body.Close()
_ = file.Close()
cancelDownloadCTX() cancelDownloadCTX()
if issue.NumberOfTotalResults > c.totalResults { if issue.NumberOfTotalResults > c.totalResults {
c.totalResults = issue.NumberOfTotalResults c.totalResults = issue.NumberOfTotalResults