Skip to content

Commit

Permalink
Merge pull request #771 from WGH-/fix-data-races
Browse files Browse the repository at this point in the history
Fix data races
  • Loading branch information
asciimoo authored Jun 20, 2023
2 parents ea4b1dd + 2dc4c6a commit a6e3d81
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
OUT="$(gofmt -l -d ./)"; test -z "$OUT" || (echo "$OUT" && return 1)
golint -set_exit_status
go vet -v ./...
go test -race -v -coverprofile=coverage.txt -covermode=atomic ./
go test -race -v -coverprofile=coverage.txt -covermode=atomic ./...
build:
name: Build ${{matrix.go}}
Expand Down
13 changes: 13 additions & 0 deletions colly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,19 @@ func TestRedirect(t *testing.T) {
c.Visit(ts.URL + "/redirect")
}

func TestIssue594(t *testing.T) {
// This is a regression test for a data race bug. There's no
// assertions because it's meant to be used with race detector
ts := newTestServer()
defer ts.Close()

c := NewCollector()
// if timeout is set, this bug is not triggered
c.SetClient(&http.Client{Timeout: 0 * time.Second})

c.Visit(ts.URL)
}

func TestRedirectWithDisallowedURLs(t *testing.T) {
ts := newTestServer()
defer ts.Close()
Expand Down
8 changes: 5 additions & 3 deletions http_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,12 @@ func (h *httpBackend) Do(request *http.Request, bodySize int, checkHeadersFunc c
return nil, err
}
defer res.Body.Close()

finalRequest := request
if res.Request != nil {
*request = *res.Request
finalRequest = res.Request
}
if !checkHeadersFunc(request, res.StatusCode, res.Header) {
if !checkHeadersFunc(finalRequest, res.StatusCode, res.Header) {
// closing res.Body (see defer above) without reading it aborts
// the download
return nil, ErrAbortedAfterHeaders
Expand All @@ -200,7 +202,7 @@ func (h *httpBackend) Do(request *http.Request, bodySize int, checkHeadersFunc c
bodyReader = io.LimitReader(bodyReader, int64(bodySize))
}
contentEncoding := strings.ToLower(res.Header.Get("Content-Encoding"))
if !res.Uncompressed && (strings.Contains(contentEncoding, "gzip") || (contentEncoding == "" && strings.Contains(strings.ToLower(res.Header.Get("Content-Type")), "gzip")) || strings.HasSuffix(strings.ToLower(request.URL.Path), ".xml.gz")) {
if !res.Uncompressed && (strings.Contains(contentEncoding, "gzip") || (contentEncoding == "" && strings.Contains(strings.ToLower(res.Header.Get("Content-Type")), "gzip")) || strings.HasSuffix(strings.ToLower(finalRequest.URL.Path), ".xml.gz")) {
bodyReader, err = gzip.NewReader(bodyReader)
if err != nil {
return nil, err
Expand Down
7 changes: 7 additions & 0 deletions queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math/rand"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"
Expand All @@ -16,6 +17,8 @@ func TestQueue(t *testing.T) {
defer server.Close()

rng := rand.New(rand.NewSource(12387123712321232))
var rngMu sync.Mutex

var (
items uint32
requests uint32
Expand All @@ -28,7 +31,9 @@ func TestQueue(t *testing.T) {
panic(err)
}
put := func() {
rngMu.Lock()
t := time.Duration(rng.Intn(50)) * time.Microsecond
rngMu.Unlock()
url := server.URL + "/delay?t=" + t.String()
atomic.AddUint32(&items, 1)
q.AddURL(url)
Expand All @@ -49,7 +54,9 @@ func TestQueue(t *testing.T) {
} else {
atomic.AddUint32(&failure, 1)
}
rngMu.Lock()
toss := rng.Intn(2) == 0
rngMu.Unlock()
if toss {
put()
}
Expand Down

0 comments on commit a6e3d81

Please sign in to comment.