Skip to content

Commit

Permalink
Fix race condition on httpSmartSubTransport
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
  • Loading branch information
Paulo Gomes committed Mar 15, 2022
1 parent 1664d8e commit e98bd36
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions pkg/git/libgit2/managed/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ type httpSmartSubtransportStream struct {
sentRequest bool
recvReply sync.WaitGroup
httpError error
m sync.RWMutex
}

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

self.recvReply.Wait()

self.m.RLock()
defer self.m.RUnlock()
if self.httpError != nil {
return 0, self.httpError
}
Expand All @@ -252,6 +255,8 @@ func (self *httpSmartSubtransportStream) Read(buf []byte) (int, error) {
}

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

func (self *httpSmartSubtransportStream) sendRequestBackground() {
go func() {
self.httpError = self.sendRequest()
err := self.sendRequest()

self.m.Lock()
self.httpError = err
self.m.Unlock()
}()
self.sentRequest = true
}
Expand Down Expand Up @@ -299,33 +308,29 @@ func (self *httpSmartSubtransportStream) sendRequest() error {
}
}

for {
req := &http.Request{
Method: self.req.Method,
URL: self.req.URL,
Header: self.req.Header,
}
if req.Method == "POST" {
req.Body = self.reader
req.ContentLength = -1
}

req.SetBasicAuth(userName, password)
resp, err = self.client.Do(req)
if err != nil {
return err
}
req := &http.Request{
Method: self.req.Method,
URL: self.req.URL,
Header: self.req.Header,
}
if req.Method == "POST" {
req.Body = self.reader
req.ContentLength = -1
}

if resp.StatusCode == http.StatusOK {
break
}
req.SetBasicAuth(userName, password)
resp, err = self.client.Do(req)
if err != nil {
return err
}

io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
return fmt.Errorf("Unhandled HTTP error %s", resp.Status)
if resp.StatusCode == http.StatusOK {
self.resp = resp
self.sentRequest = true
return nil
}

self.sentRequest = true
self.resp = resp
return nil
io.Copy(ioutil.Discard, resp.Body)
defer resp.Body.Close()
return fmt.Errorf("Unhandled HTTP error %s", resp.Status)
}

0 comments on commit e98bd36

Please sign in to comment.