aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJavi Fontan <jfontan@gmail.com>2019-03-01 15:33:49 +0100
committerJavi Fontan <jfontan@gmail.com>2019-03-01 15:33:49 +0100
commitfae29fbd5895363ef5464cc12029db85a1a24529 (patch)
tree451661799b2619458b36359a79c7ee58f5d8a715
parent0106daba16494c33d330168e38c8a610b7a9b32c (diff)
downloadgo-git-fae29fbd5895363ef5464cc12029db85a1a24529.tar.gz
git: fix goroutine block while pushing a remote
On session.ReceivePack error the gororutine doing the encoding got blocked either writing objects to the pipe or sending error to the done channel. The problem did not cause a perceived problem but left blocked goroutines. Signed-off-by: Javi Fontan <jfontan@gmail.com>
-rw-r--r--remote.go9
-rw-r--r--remote_test.go9
2 files changed, 17 insertions, 1 deletions
diff --git a/remote.go b/remote.go
index de537ce..8060409 100644
--- a/remote.go
+++ b/remote.go
@@ -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 {
@@ -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
}
diff --git a/remote_test.go b/remote_test.go
index 28b0a3a..58a0598 100644
--- a/remote_test.go
+++ b/remote_test.go
@@ -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"
@@ -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) {