Skip to content

Commit 5b1298a

Browse files
authored
Merge pull request #315 from hashicorp/fix-req-race-condition
HttpGetter: Avoid reusing the same http.Request
2 parents 8724413 + 3cca945 commit 5b1298a

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

.circleci/config.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ commands:
1515
type: string
1616
platform:
1717
type: string
18+
govet:
19+
type: string
20+
default: ""
1821
steps:
1922
- run:
2023
name: "Run go tests"
2124
command: |
2225
PACKAGE_NAMES=$(go list ./... | circleci tests split --split-by=timings --timings-type=classname)
2326
echo "Running $(echo $PACKAGE_NAMES | wc -w) packages"
2427
echo $PACKAGE_NAMES
25-
<< parameters.cmd >> --format=short-verbose --junitfile $TEST_RESULTS_PATH/go-getter/gotestsum-report.xml -- -p 2 -cover -coverprofile=<< parameters.platform >>_cov_$CIRCLE_NODE_INDEX.part $PACKAGE_NAMES
28+
<< parameters.cmd >> --format=short-verbose --junitfile $TEST_RESULTS_PATH/go-getter/gotestsum-report.xml -- -p 2 -cover -race -vet=<< parameters.govet >> -coverprofile=<< parameters.platform >>_cov_$CIRCLE_NODE_INDEX.part $PACKAGE_NAMES
2629
2730
jobs:
2831
linux-tests:
@@ -140,6 +143,9 @@ jobs:
140143
- run-gotests:
141144
cmd: "./gotestsum.exe"
142145
platform: "win"
146+
# Otherwise gcc is required for race detector
147+
# See https://github.com/golang/go/issues/27089
148+
govet: "off"
143149

144150
# Save coverage report parts
145151
- persist_to_workspace:

get_http.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ func (g *HttpGetter) GetFile(dst string, src *url.URL) error {
181181
if fi, err := f.Stat(); err == nil {
182182
if _, err = f.Seek(0, io.SeekEnd); err == nil {
183183
currentFileSize = fi.Size()
184-
req.Header.Set("Range", fmt.Sprintf("bytes=%d-", currentFileSize))
185184
if currentFileSize >= headResp.ContentLength {
186185
// file already present
187186
return nil
@@ -191,7 +190,17 @@ func (g *HttpGetter) GetFile(dst string, src *url.URL) error {
191190
}
192191
}
193192
}
194-
req.Method = "GET"
193+
194+
req, err = http.NewRequest("GET", src.String(), nil)
195+
if err != nil {
196+
return err
197+
}
198+
if g.Header != nil {
199+
req.Header = g.Header.Clone()
200+
}
201+
if currentFileSize > 0 {
202+
req.Header.Set("Range", fmt.Sprintf("bytes=%d-", currentFileSize))
203+
}
195204

196205
resp, err := g.Client.Do(req)
197206
if err != nil {

get_http_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,23 @@ func TestHttpGetter_file(t *testing.T) {
298298
assertContents(t, dst, "Hello\n")
299299
}
300300

301+
// TestHttpGetter_http2server tests that http.Request is not reused
302+
// between HEAD & GET, which would lead to race condition in HTTP/2.
303+
// This test is only meaningful for the race detector (go test -race).
304+
func TestHttpGetter_http2server(t *testing.T) {
305+
g := new(HttpGetter)
306+
src, err := url.Parse("https://releases.hashicorp.com/terraform/0.14.0/terraform_0.14.0_SHA256SUMS")
307+
if err != nil {
308+
t.Fatal(err)
309+
}
310+
dst := tempTestFile(t)
311+
312+
err = g.GetFile(dst, src)
313+
if err != nil {
314+
t.Fatal(err)
315+
}
316+
}
317+
301318
func TestHttpGetter_auth(t *testing.T) {
302319
ln := testHttpServer(t)
303320
defer ln.Close()

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/cheggaaa/pb v1.0.27
88
github.com/davecgh/go-spew v1.1.1 // indirect
99
github.com/fatih/color v1.7.0 // indirect
10-
github.com/hashicorp/go-cleanhttp v0.5.0
10+
github.com/hashicorp/go-cleanhttp v0.5.2
1111
github.com/hashicorp/go-safetemp v1.0.0
1212
github.com/hashicorp/go-version v1.1.0
1313
github.com/klauspost/compress v1.11.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OI
4242
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
4343
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
4444
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
45-
github.com/hashicorp/go-cleanhttp v0.5.0 h1:wvCrVc9TjDls6+YGAF2hAifE1E5U1+b4tH6KdvN3Gig=
46-
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
45+
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
46+
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
4747
github.com/hashicorp/go-safetemp v1.0.0 h1:2HR189eFNrjHQyENnQMMpCiBAsRxzbTMIgBhEyExpmo=
4848
github.com/hashicorp/go-safetemp v1.0.0/go.mod h1:oaerMy3BhqiTbVye6QuFhFtIceqFoDHxNAB65b+Rj1I=
4949
github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0=

0 commit comments

Comments
 (0)