Skip to content

Commit 413e380

Browse files
authored
Merge pull request #701 from cezarsa/fixrace
testing: fix data race when reading container state in some handlers
2 parents 5030de4 + 883a709 commit 413e380

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

testing/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,8 @@ func (s *DockerServer) topContainer(w http.ResponseWriter, r *http.Request) {
640640
http.Error(w, err.Error(), http.StatusNotFound)
641641
return
642642
}
643+
s.cMut.RLock()
644+
defer s.cMut.RUnlock()
643645
if !container.State.Running {
644646
w.WriteHeader(http.StatusInternalServerError)
645647
fmt.Fprintf(w, "Container %s is not running", id)
@@ -787,11 +789,13 @@ func (s *DockerServer) attachContainer(w http.ResponseWriter, r *http.Request) {
787789
}()
788790
}
789791
outStream := stdcopy.NewStdWriter(conn, stdcopy.Stdout)
792+
s.cMut.RLock()
790793
if container.State.Running {
791794
fmt.Fprintf(outStream, "Container is running\n")
792795
} else {
793796
fmt.Fprintf(outStream, "Container is not running\n")
794797
}
798+
s.cMut.RUnlock()
795799
fmt.Fprintln(outStream, "What happened?")
796800
fmt.Fprintln(outStream, "Something happened")
797801
wg.Wait()
@@ -919,11 +923,13 @@ func (s *DockerServer) logContainer(w http.ResponseWriter, r *http.Request) {
919923
}
920924
w.Header().Set("Content-Type", "application/vnd.docker.raw-stream")
921925
w.WriteHeader(http.StatusOK)
926+
s.cMut.RLock()
922927
if container.State.Running {
923928
fmt.Fprintf(w, "Container is running\n")
924929
} else {
925930
fmt.Fprintf(w, "Container is not running\n")
926931
}
932+
s.cMut.RUnlock()
927933
fmt.Fprintln(w, "What happened?")
928934
fmt.Fprintln(w, "Something happened")
929935
if r.URL.Query().Get("follow") == "1" {

0 commit comments

Comments
 (0)