Skip to content

Commit

Permalink
fix: correctly closing the spooled temp files when errors pop up that…
Browse files Browse the repository at this point in the history
… prevent us from sending the full record to the channel.

This is likely the case of ballooning temp folders, as we only return an error and don't send the full record, we can't close the record which contains the temp file.
  • Loading branch information
NGTmeaty authored and CorentinB committed Oct 4, 2024
1 parent df2baba commit 8e11ca2
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ func (d *customDialer) writeWARCFromConnection(reqPipe, respPipe *io.PipeReader,

// Make sure we close the WARC content buffers
for record := range recordChan {
record.Content.Close()
// CHeck if there's an error when closing the content and send to channel if so.
err := record.Content.Close()
if err != nil {
d.client.ErrChan <- &Error{Err: err}
}
}

return
Expand All @@ -287,7 +291,10 @@ func (d *customDialer) writeWARCFromConnection(reqPipe, respPipe *io.PipeReader,

// Make sure we close the WARC content buffers
for _, record := range batch.Records {
record.Content.Close()
err := record.Content.Close()
if err != nil {
d.client.ErrChan <- &Error{Err: err}
}
}

return
Expand Down Expand Up @@ -361,11 +368,13 @@ func (d *customDialer) readResponse(respPipe *io.PipeReader, warcTargetURIChanne
// Read the response from the pipe
bytesCopied, err := io.Copy(responseRecord.Content, respPipe)
if err != nil {
responseRecord.Content.Close()
return err
}

resp, err := http.ReadResponse(bufio.NewReader(responseRecord.Content), nil)
if err != nil {
responseRecord.Content.Close()
return err
}

Expand All @@ -383,10 +392,14 @@ func (d *customDialer) readResponse(respPipe *io.PipeReader, warcTargetURIChanne
// Calculate the WARC-Payload-Digest
payloadDigest := GetSHA1(resp.Body)
if strings.HasPrefix(payloadDigest, "ERROR: ") {
responseRecord.Content.Close()
// This should _never_ happen.
return fmt.Errorf("SHA1 ran into an unrecoverable error: %s url: %s", payloadDigest, warcTargetURI)
}
resp.Body.Close()
err = resp.Body.Close()
if err != nil {
return err
}
responseRecord.Header.Set("WARC-Payload-Digest", "sha1:"+payloadDigest)

// Write revisit record if local or CDX dedupe is activated
Expand Down Expand Up @@ -503,7 +516,10 @@ func (d *customDialer) readResponse(respPipe *io.PipeReader, warcTargetURIChanne
}

// Close old buffer
responseRecord.Content.Close()
err = responseRecord.Content.Close()
if err != nil {
return err
}
responseRecord.Content = tempBuffer
}

Expand Down

0 comments on commit 8e11ca2

Please sign in to comment.