Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly closing records when errors occur before sending to channel #54

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading