Skip to content

Commit ddfd07a

Browse files
authored
Merge pull request #711 from cezarsa/fixrace
testing: fix race condition reading data from containers
2 parents f21b764 + 26cad7b commit ddfd07a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

testing/server.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,10 @@ func (s *DockerServer) renameContainer(w http.ResponseWriter, r *http.Request) {
547547
http.Error(w, err.Error(), http.StatusNotFound)
548548
return
549549
}
550-
copy := *container
551-
copy.Name = r.URL.Query().Get("name")
552550
s.cMut.Lock()
553551
defer s.cMut.Unlock()
552+
copy := *container
553+
copy.Name = r.URL.Query().Get("name")
554554
if s.containers[index].ID == copy.ID {
555555
s.containers[index] = &copy
556556
}
@@ -566,6 +566,8 @@ func (s *DockerServer) inspectContainer(w http.ResponseWriter, r *http.Request)
566566
}
567567
w.Header().Set("Content-Type", "application/json")
568568
w.WriteHeader(http.StatusOK)
569+
s.cMut.RLock()
570+
defer s.cMut.RUnlock()
569571
json.NewEncoder(w).Encode(container)
570572
}
571573

@@ -820,16 +822,18 @@ func (s *DockerServer) waitContainer(w http.ResponseWriter, r *http.Request) {
820822
http.Error(w, err.Error(), http.StatusNotFound)
821823
return
822824
}
825+
var exitCode int
823826
for {
824827
time.Sleep(1e6)
825828
s.cMut.RLock()
826829
if !container.State.Running {
830+
exitCode = container.State.ExitCode
827831
s.cMut.RUnlock()
828832
break
829833
}
830834
s.cMut.RUnlock()
831835
}
832-
result := map[string]int{"StatusCode": container.State.ExitCode}
836+
result := map[string]int{"StatusCode": exitCode}
833837
json.NewEncoder(w).Encode(result)
834838
}
835839

@@ -1153,7 +1157,9 @@ func (s *DockerServer) createExecContainer(w http.ResponseWriter, r *http.Reques
11531157
}
11541158

11551159
execID := s.generateID()
1160+
s.cMut.Lock()
11561161
container.ExecIDs = append(container.ExecIDs, execID)
1162+
s.cMut.Unlock()
11571163

11581164
exec := docker.ExecInspect{
11591165
ID: execID,

0 commit comments

Comments
 (0)