Skip to content

Commit

Permalink
event: fix yet another data race in event monitoring
Browse files Browse the repository at this point in the history
This is the race I detected locally:

==================
WARNING: DATA RACE
Write at 0x00c4203cda88 by goroutine 55:
  github.com/fsouza/go-dockerclient.(*eventMonitoringState).closeListeners()
      /Users/fsouza/src/github.com/fsouza/go-dockerclient/event.go:150 +0xdc
  github.com/fsouza/go-dockerclient.(*eventMonitoringState).disableEventMonitoring()
      /Users/fsouza/src/github.com/fsouza/go-dockerclient/event.go:179 +0x91
  github.com/fsouza/go-dockerclient.(*eventMonitoringState).monitorEvents()
      /Users/fsouza/src/github.com/fsouza/go-dockerclient/event.go:209 +0x23d

Previous read at 0x00c4203cda88 by goroutine 58:
  github.com/fsouza/go-dockerclient.(*Client).RemoveEventListener()
      /Users/fsouza/src/github.com/fsouza/go-dockerclient/event.go:112 +0xc7
  github.com/fsouza/go-dockerclient.testEventListeners.func2()
      /Users/fsouza/src/github.com/fsouza/go-dockerclient/event_test.go:235 +0x5b
  github.com/fsouza/go-dockerclient.testEventListeners()
      /Users/fsouza/src/github.com/fsouza/go-dockerclient/event_test.go:258 +0x191b
  github.com/fsouza/go-dockerclient.TestTLSEventListeners()
      /Users/fsouza/src/github.com/fsouza/go-dockerclient/event_test.go:50 +0xa6
  testing.tRunner()
      /Users/fsouza/.gimme/versions/go1.7beta2.darwin.amd64/src/testing/testing.go:610 +0xc9

Goroutine 55 (running) created at:
  github.com/fsouza/go-dockerclient.(*eventMonitoringState).enableEventMonitoring()
      /Users/fsouza/src/github.com/fsouza/go-dockerclient/event.go:170 +0x1cf
  github.com/fsouza/go-dockerclient.(*Client).AddEventListener()
      /Users/fsouza/src/github.com/fsouza/go-dockerclient/event.go:94 +0x116
  github.com/fsouza/go-dockerclient.testEventListeners()
      /Users/fsouza/src/github.com/fsouza/go-dockerclient/event_test.go:240 +0x16bd
  github.com/fsouza/go-dockerclient.TestTLSEventListeners()
      /Users/fsouza/src/github.com/fsouza/go-dockerclient/event_test.go:50 +0xa6
  testing.tRunner()
      /Users/fsouza/.gimme/versions/go1.7beta2.darwin.amd64/src/testing/testing.go:610 +0xc9

Goroutine 58 (running) created at:
  testing.(*T).Run()
      /Users/fsouza/.gimme/versions/go1.7beta2.darwin.amd64/src/testing/testing.go:646 +0x52f
  testing.RunTests.func1()
      /Users/fsouza/.gimme/versions/go1.7beta2.darwin.amd64/src/testing/testing.go:793 +0xb9
  testing.tRunner()
      /Users/fsouza/.gimme/versions/go1.7beta2.darwin.amd64/src/testing/testing.go:610 +0xc9
  testing.RunTests()
      /Users/fsouza/.gimme/versions/go1.7beta2.darwin.amd64/src/testing/testing.go:799 +0x4b5
  testing.(*M).Run()
      /Users/fsouza/.gimme/versions/go1.7beta2.darwin.amd64/src/testing/testing.go:743 +0x12f
  main.main()
      github.com/fsouza/go-dockerclient/_test/_testmain.go:456 +0x1b4
==================
  • Loading branch information
fsouza committed Jun 24, 2016
1 parent 66550b9 commit 60efda8
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c *Client) RemoveEventListener(listener chan *APIEvents) error {
if err != nil {
return err
}
if len(c.eventMonitor.listeners) == 0 {
if c.eventMonitor.listernersCount() == 0 {
c.eventMonitor.disableEventMonitoring()
}
return nil
Expand Down Expand Up @@ -150,6 +150,12 @@ func (eventState *eventMonitoringState) closeListeners() {
eventState.listeners = nil
}

func (eventState *eventMonitoringState) listernersCount() int {
eventState.RLock()
defer eventState.RUnlock()
return len(eventState.listeners)
}

func listenerExists(a chan<- *APIEvents, list *[]chan<- *APIEvents) bool {
for _, b := range *list {
if b == a {
Expand Down

0 comments on commit 60efda8

Please sign in to comment.