Skip to content

Commit d1a7e5d

Browse files
Paulo Gomesdarkowlzz
authored andcommitted
Fix race condition on httpSmartSubTransport
Signed-off-by: Paulo Gomes <[email protected]>
1 parent 822788b commit d1a7e5d

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

pkg/git/libgit2/managed/http.go

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ type httpSmartSubtransportStream struct {
217217
sentRequest bool
218218
recvReply sync.WaitGroup
219219
httpError error
220+
m sync.RWMutex
220221
}
221222

222223
func newManagedHttpStream(owner *httpSmartSubtransport, req *http.Request, client *http.Client) *httpSmartSubtransportStream {
@@ -244,6 +245,8 @@ func (self *httpSmartSubtransportStream) Read(buf []byte) (int, error) {
244245

245246
self.recvReply.Wait()
246247

248+
self.m.RLock()
249+
defer self.m.RUnlock()
247250
if self.httpError != nil {
248251
return 0, self.httpError
249252
}
@@ -252,6 +255,8 @@ func (self *httpSmartSubtransportStream) Read(buf []byte) (int, error) {
252255
}
253256

254257
func (self *httpSmartSubtransportStream) Write(buf []byte) (int, error) {
258+
self.m.RLock()
259+
defer self.m.RUnlock()
255260
if self.httpError != nil {
256261
return 0, self.httpError
257262
}
@@ -266,7 +271,11 @@ func (self *httpSmartSubtransportStream) Free() {
266271

267272
func (self *httpSmartSubtransportStream) sendRequestBackground() {
268273
go func() {
269-
self.httpError = self.sendRequest()
274+
err := self.sendRequest()
275+
276+
self.m.Lock()
277+
self.httpError = err
278+
self.m.Unlock()
270279
}()
271280
self.sentRequest = true
272281
}
@@ -299,33 +308,29 @@ func (self *httpSmartSubtransportStream) sendRequest() error {
299308
}
300309
}
301310

302-
for {
303-
req := &http.Request{
304-
Method: self.req.Method,
305-
URL: self.req.URL,
306-
Header: self.req.Header,
307-
}
308-
if req.Method == "POST" {
309-
req.Body = self.reader
310-
req.ContentLength = -1
311-
}
312-
313-
req.SetBasicAuth(userName, password)
314-
resp, err = self.client.Do(req)
315-
if err != nil {
316-
return err
317-
}
311+
req := &http.Request{
312+
Method: self.req.Method,
313+
URL: self.req.URL,
314+
Header: self.req.Header,
315+
}
316+
if req.Method == "POST" {
317+
req.Body = self.reader
318+
req.ContentLength = -1
319+
}
318320

319-
if resp.StatusCode == http.StatusOK {
320-
break
321-
}
321+
req.SetBasicAuth(userName, password)
322+
resp, err = self.client.Do(req)
323+
if err != nil {
324+
return err
325+
}
322326

323-
io.Copy(ioutil.Discard, resp.Body)
324-
resp.Body.Close()
325-
return fmt.Errorf("Unhandled HTTP error %s", resp.Status)
327+
if resp.StatusCode == http.StatusOK {
328+
self.resp = resp
329+
self.sentRequest = true
330+
return nil
326331
}
327332

328-
self.sentRequest = true
329-
self.resp = resp
330-
return nil
333+
io.Copy(ioutil.Discard, resp.Body)
334+
defer resp.Body.Close()
335+
return fmt.Errorf("Unhandled HTTP error %s", resp.Status)
331336
}

0 commit comments

Comments
 (0)