Skip to content

Commit

Permalink
Wait until stdout and stderr are fully consumed before calling cmd.Wa…
Browse files Browse the repository at this point in the history
…it()
  • Loading branch information
beefsack committed Aug 26, 2020
1 parent 477b831 commit 81a7562
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}()

// We must consume stdout and stderr before `cmd.Wait()` as per
// doc and example at https://golang.org/pkg/os/exec/#Cmd.StdoutPipe
wg.Wait()

// Wait for the subprocess to complete
cmdErr := cmd.Wait()
if cmdErr != nil {
Expand All @@ -106,9 +110,6 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("error running subprocess: %v", err)
respError(w)
}
// Also wait for all of our goroutines to finish, in case there was
// buffering
wg.Wait()

// Write stdout as the response body
_, err = w.Write(stdout)
Expand Down

0 comments on commit 81a7562

Please sign in to comment.