Skip to content

Commit

Permalink
Merge pull request #711 from cezarsa/fixrace
Browse files Browse the repository at this point in the history
testing: fix race condition reading data from containers
  • Loading branch information
fsouza authored Feb 1, 2018
2 parents f21b764 + 26cad7b commit ddfd07a
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions testing/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,10 @@ func (s *DockerServer) renameContainer(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
copy := *container
copy.Name = r.URL.Query().Get("name")
s.cMut.Lock()
defer s.cMut.Unlock()
copy := *container
copy.Name = r.URL.Query().Get("name")
if s.containers[index].ID == copy.ID {
s.containers[index] = &copy
}
Expand All @@ -566,6 +566,8 @@ func (s *DockerServer) inspectContainer(w http.ResponseWriter, r *http.Request)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
s.cMut.RLock()
defer s.cMut.RUnlock()
json.NewEncoder(w).Encode(container)
}

Expand Down Expand Up @@ -820,16 +822,18 @@ func (s *DockerServer) waitContainer(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
var exitCode int
for {
time.Sleep(1e6)
s.cMut.RLock()
if !container.State.Running {
exitCode = container.State.ExitCode
s.cMut.RUnlock()
break
}
s.cMut.RUnlock()
}
result := map[string]int{"StatusCode": container.State.ExitCode}
result := map[string]int{"StatusCode": exitCode}
json.NewEncoder(w).Encode(result)
}

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

execID := s.generateID()
s.cMut.Lock()
container.ExecIDs = append(container.ExecIDs, execID)
s.cMut.Unlock()

exec := docker.ExecInspect{
ID: execID,
Expand Down

0 comments on commit ddfd07a

Please sign in to comment.