Skip to content

Commit

Permalink
testing: fix data race when reading container state in some handlers
Browse files Browse the repository at this point in the history
Example of data race found:

```
==================
WARNING: DATA RACE
Write at 0x00c420df4468 by goroutine 559:
  github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing.(*DockerServer).stopContainer()
      /home/travis/gopath/src/github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing/server.go:724 +0x15a
  github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing.(*DockerServer).(github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing.stopContainer)-fm()
      /home/travis/gopath/src/github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing/server.go:169 +0x5f
  github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing.(*DockerServer).handlerWrapper.func1()
      /home/travis/gopath/src/github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing/server.go:379 +0x70a
  net/http.HandlerFunc.ServeHTTP()
      /home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:1918 +0x51
  github.com/tsuru/tsuru/vendor/github.com/gorilla/mux.(*Router).ServeHTTP()
      /home/travis/gopath/src/github.com/tsuru/tsuru/vendor/github.com/gorilla/mux/mux.go:133 +0x18a
  github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing.(*DockerServer).ServeHTTP()
      /home/travis/gopath/src/github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing/server.go:340 +0x271
  net/http.serverHandler.ServeHTTP()
      /home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:2619 +0xbc
  net/http.(*conn).serve()
      /home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:1801 +0x83b
Previous read at 0x00c420df4468 by goroutine 165:
  github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing.(*DockerServer).attachContainer()
      /home/travis/gopath/src/github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing/server.go:790 +0x356
  github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing.(*DockerServer).(github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing.attachContainer)-fm()
      /home/travis/gopath/src/github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing/server.go:174 +0x5f
  github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing.(*DockerServer).handlerWrapper.func1()
      /home/travis/gopath/src/github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing/server.go:379 +0x70a
  net/http.HandlerFunc.ServeHTTP()
      /home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:1918 +0x51
  github.com/tsuru/tsuru/vendor/github.com/gorilla/mux.(*Router).ServeHTTP()
      /home/travis/gopath/src/github.com/tsuru/tsuru/vendor/github.com/gorilla/mux/mux.go:133 +0x18a
  github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing.(*DockerServer).ServeHTTP()
      /home/travis/gopath/src/github.com/tsuru/tsuru/vendor/github.com/fsouza/go-dockerclient/testing/server.go:340 +0x271
  net/http.serverHandler.ServeHTTP()
      /home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:2619 +0xbc
  net/http.(*conn).serve()
      /home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:1801 +0x83b
Goroutine 559 (running) created at:
  net/http.(*Server).Serve()
      /home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:2720 +0x37c
  net/http.Serve()
      /home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:2323 +0xe2
Goroutine 165 (running) created at:
  net/http.(*Server).Serve()
      /home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:2720 +0x37c
  net/http.Serve()
      /home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:2323 +0xe2
==================
```
  • Loading branch information
cezarsa committed Dec 12, 2017
1 parent 5030de4 commit 883a709
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 883a709

Please sign in to comment.