Skip to content

Commit

Permalink
fix: race in v2 client
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Aug 2, 2020
1 parent 9ce4416 commit 1a55bd7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions dsl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"time"

"github.com/pact-foundation/pact-go/client"
Expand Down Expand Up @@ -182,7 +183,7 @@ func (p *PactClient) VerifyProvider(request types.VerifyRequest) ([]types.Provid
}

// Buffered channel: wait for all reading to complete
done := make(chan struct{}, 2)
var wg sync.WaitGroup
verifications := []string{}
var stdErr strings.Builder

Expand All @@ -191,21 +192,23 @@ func (p *PactClient) VerifyProvider(request types.VerifyRequest) ([]types.Provid
// See https://github.com/pact-foundation/pact-go/issues/88#issuecomment-404686337
stdOutScanner := bufio.NewScanner(stdOutPipe)
go func() {
wg.Add(1)
for stdOutScanner.Scan() {
verifications = append(verifications, stdOutScanner.Text())
}

done <- struct{}{}
wg.Done()
}()

// Scrape errors
stdErrScanner := bufio.NewScanner(stdErrPipe)
go func() {
wg.Add(1)
for stdErrScanner.Scan() {
stdErr.WriteString(fmt.Sprintf("%s\n", stdErrScanner.Text()))
}

done <- struct{}{}
wg.Done()
}()

err = cmd.Start()
Expand All @@ -214,9 +217,8 @@ func (p *PactClient) VerifyProvider(request types.VerifyRequest) ([]types.Provid
}

// Wait for watch goroutine before Cmd.Wait(), race condition!
<-done

err = cmd.Wait()
wg.Wait()

var verification types.ProviderVerifierResponse
for _, v := range verifications {
Expand Down

0 comments on commit 1a55bd7

Please sign in to comment.