Skip to content

Commit

Permalink
Merge pull request #701 from cezarsa/fixrace
Browse files Browse the repository at this point in the history
testing: fix data race when reading container state in some handlers
  • Loading branch information
fsouza authored Dec 12, 2017
2 parents 5030de4 + 883a709 commit 413e380
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions testing/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@ func (s *DockerServer) topContainer(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
s.cMut.RLock()
defer s.cMut.RUnlock()
if !container.State.Running {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Container %s is not running", id)
Expand Down Expand Up @@ -787,11 +789,13 @@ func (s *DockerServer) attachContainer(w http.ResponseWriter, r *http.Request) {
}()
}
outStream := stdcopy.NewStdWriter(conn, stdcopy.Stdout)
s.cMut.RLock()
if container.State.Running {
fmt.Fprintf(outStream, "Container is running\n")
} else {
fmt.Fprintf(outStream, "Container is not running\n")
}
s.cMut.RUnlock()
fmt.Fprintln(outStream, "What happened?")
fmt.Fprintln(outStream, "Something happened")
wg.Wait()
Expand Down Expand Up @@ -919,11 +923,13 @@ func (s *DockerServer) logContainer(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", "application/vnd.docker.raw-stream")
w.WriteHeader(http.StatusOK)
s.cMut.RLock()
if container.State.Running {
fmt.Fprintf(w, "Container is running\n")
} else {
fmt.Fprintf(w, "Container is not running\n")
}
s.cMut.RUnlock()
fmt.Fprintln(w, "What happened?")
fmt.Fprintln(w, "Something happened")
if r.URL.Query().Get("follow") == "1" {
Expand Down

0 comments on commit 413e380

Please sign in to comment.