Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

git: fix goroutine block while pushing a remote #1080

Merged
merged 1 commit into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,12 @@ func pushHashes(
if err != nil {
return nil, err
}
done := make(chan error)

// Set buffer size to 1 so the error message can be written when
// ReceivePack fails. Otherwise the goroutine will be blocked writing
// to the channel.
done := make(chan error, 1)

go func() {
e := packfile.NewEncoder(wr, s, useRefDeltas)
if _, err := e.Encode(hs, config.Pack.Window); err != nil {
Expand All @@ -1033,6 +1038,8 @@ func pushHashes(

rs, err := sess.ReceivePack(ctx, req)
if err != nil {
// close the pipe to unlock encode write
_ = rd.Close()
return nil, err
}

Expand Down
9 changes: 9 additions & 0 deletions remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"io/ioutil"
"os"
"runtime"
"time"

"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
Expand Down Expand Up @@ -448,10 +450,17 @@ func (s *RemoteSuite) TestPushContext(c *C) {
ctx, cancel := context.WithCancel(context.Background())
cancel()

numGoroutines := runtime.NumGoroutine()

err = r.PushContext(ctx, &PushOptions{
RefSpecs: []config.RefSpec{"refs/tags/*:refs/tags/*"},
})
c.Assert(err, NotNil)

// let the goroutine from pushHashes finish and check that the number of
// goroutines is the same as before
time.Sleep(100 * time.Millisecond)
c.Assert(runtime.NumGoroutine(), Equals, numGoroutines)
}

func (s *RemoteSuite) TestPushTags(c *C) {
Expand Down