CyCoreSystems / ari

Golang Asterisk REST Interface (ARI) library
Apache License 2.0
189 stars 73 forks source link

Race condition: Bus should lock subscriptions array while reading in Send method #94

Closed devishot closed 5 years ago

devishot commented 5 years ago

I have ran into a problem with the Bus.Send() method in stdbus/ implementation. It raised error when tried read .key from one of the subscriptions which was nil.

This bug may happen when run concurrently: a) remove subcription from array of subscriptions in bus and b) loop over this array of subscriptions in Send() method.

For check this problem, you can run single test with -race flag for execute with Go's race detector:

cd stdbus 
go test -race -run TestEvents

Output will be:

==================
WARNING: DATA RACE
Read at 0x00c00006f350 by goroutine 11:
  github.com/devishot/ari/stdbus.(*bus).Send()
      C:/Users/stamkulov/Documents/Kolesa/go/src/github.com/devishot/ari/stdbus/bus.go:50 +0x59
  github.com/devishot/ari/stdbus.TestEventsDataRace.func2()
      C:/Users/stamkulov/Documents/Kolesa/go/src/github.com/devishot/ari/stdbus/bus_test.go:208 +0xaa

Previous write at 0x00c00006f350 by goroutine 10:
  github.com/devishot/ari/stdbus.(*bus).add()
      C:/Users/stamkulov/Documents/Kolesa/go/src/github.com/devishot/ari/stdbus/bus.go:82 +0xe8
  github.com/devishot/ari/stdbus.(*bus).Subscribe()
      C:/Users/stamkulov/Documents/Kolesa/go/src/github.com/devishot/ari/stdbus/bus.go:75 +0x186
  github.com/devishot/ari/stdbus.TestEventsDataRace.func1()
      C:/Users/stamkulov/Documents/Kolesa/go/src/github.com/devishot/ari/stdbus/bus_test.go:194 +0x21a

Goroutine 11 (running) created at:
  github.com/devishot/ari/stdbus.TestEventsDataRace()
      C:/Users/stamkulov/Documents/Kolesa/go/src/github.com/devishot/ari/stdbus/bus_test.go:204 +0x377
  testing.tRunner()
      C:/Go/src/testing/testing.go:827 +0x169

Goroutine 10 (running) created at:
  github.com/devishot/ari/stdbus.TestEventsDataRace()
      C:/Users/stamkulov/Documents/Kolesa/go/src/github.com/devishot/ari/stdbus/bus_test.go:187 +0x33f
  testing.tRunner()
      C:/Go/src/testing/testing.go:827 +0x169
==================
--- FAIL: TestEventsDataRace (0.00s)
    testing.go:771: race detected during execution of test
FAIL
exit status 1
FAIL    github.com/devishot/ari/stdbus  0.426s