Skip to content

Commit

Permalink
fix racing issue with using ResponsePipe.WriteTo
Browse files Browse the repository at this point in the history
* WriteTo should block until both writeResponse and writeError
  are done. Or risking parallel read / write of the error writer
  contents.
  • Loading branch information
yookoala committed Feb 7, 2018
1 parent 0eaa8c7 commit 68b6ff0
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,17 +405,23 @@ func (pipes *ResponsePipe) Close() {
// WriteTo writes the given output into http.ResponseWriter
func (pipes *ResponsePipe) WriteTo(rw http.ResponseWriter, ew io.Writer) (err error) {
chErr := make(chan error, 2)
defer close(chErr)

var wg sync.WaitGroup
wg.Add(2)

go func() {
chErr <- pipes.writeResponse(rw)
wg.Done()
}()
go func() {
chErr <- pipes.writeError(ew)
wg.Done()
}()

wg.Wait()
for i := 0; i < 2; i++ {
if err = <-chErr; err != nil {
close(chErr)
return
}
}
Expand Down

0 comments on commit 68b6ff0

Please sign in to comment.