muka / go-bluetooth

Golang bluetooth client based on bluez DBus interfaces
Apache License 2.0
653 stars 123 forks source link

data race at adapter.(*Adapter1).OnDeviceDiscovered #201

Open rajivk-slx opened 3 months ago

rajivk-slx commented 3 months ago

ubuntu 22.04, arm64

Sometimes this runs into a datarace

func TestOnDiscoverDataRace(t *testing.T) {
    a, err := adapter.GetAdapter(adapter.GetDefaultAdapterID())
    if err != nil {
        t.Fatal(err)
    }

    if err = a.FlushDevices(); err != nil {
        t.Fatal(err)
    }

    err = a.StartDiscovery()
    if err != nil {
        t.Fatal(err)
    }

    ch, discoveryCancel, err := a.OnDeviceDiscovered()
    if err != nil {
        t.Fatal(err)
    }

    cancel := func() {
        err := a.StopDiscovery()
        if err != nil {
            slog.Warn("Error stopping discovery: %s", err)
        }
        discoveryCancel()
    }

    defer cancel()
    <-ch
}
Running tool: /snap/go/current/bin/go test -timeout 300s -run ^TestOnDiscoverDataRace$ vstcl/device -v -count=1 -race

=== RUN   TestOnDiscoverDataRace
==================
WARNING: DATA RACE
Write at 0x00c0000221f0 by goroutine 7:
  runtime.recvDirect()
      /snap/go/10611/src/runtime/chan.go:348 +0x7c
  github.com/muka/go-bluetooth/bluez/profile/adapter.(*Adapter1).OnDeviceDiscovered.func2()
      /home/rkanchan/go/pkg/mod/github.com/muka/go-bluetooth@v0.0.0-20240115085408-dfdf79b8f61d/bluez/profile/adapter/adapter_discovery.go:99 +0x5c
  vstcl/device.TestOnDiscoverDataRace.func1()
      /home/rkanchan/src/call-light/src/go-sdk/lib/device/device_test.go:707 +0xa0
  runtime.deferreturn()
      /snap/go/10611/src/runtime/panic.go:602 +0x5c
  testing.tRunner()
      /snap/go/10611/src/testing/testing.go:1689 +0x180
  testing.(*T).Run.gowrap1()
      /snap/go/10611/src/testing/testing.go:1742 +0x40

Previous read at 0x00c0000221f0 by goroutine 19:
  runtime.chansend1()
      /snap/go/10611/src/runtime/chan.go:146 +0x2c
  github.com/muka/go-bluetooth/bluez/profile/adapter.(*Adapter1).OnDeviceDiscovered.func1()
      /home/rkanchan/go/pkg/mod/github.com/muka/go-bluetooth@v0.0.0-20240115085408-dfdf79b8f61d/bluez/profile/adapter/adapter_discovery.go:89 +0x354

Goroutine 7 (running) created at:
  testing.(*T).Run()
      /snap/go/10611/src/testing/testing.go:1742 +0x5e4
  testing.runTests.func1()
      /snap/go/10611/src/testing/testing.go:2161 +0x80
  testing.tRunner()
      /snap/go/10611/src/testing/testing.go:1689 +0x180
  testing.runTests()
      /snap/go/10611/src/testing/testing.go:2159 +0x6e0
  testing.(*M).Run()
      /snap/go/10611/src/testing/testing.go:2027 +0xb74
  main.main()
      _testmain.go:69 +0x294

Goroutine 19 (running) created at:
  github.com/muka/go-bluetooth/bluez/profile/adapter.(*Adapter1).OnDeviceDiscovered()
      /home/rkanchan/go/pkg/mod/github.com/muka/go-bluetooth@v0.0.0-20240115085408-dfdf79b8f61d/bluez/profile/adapter/adapter_discovery.go:37 +0x134
  vstcl/device.TestOnDiscoverDataRace()
      /home/rkanchan/src/call-light/src/go-sdk/lib/device/device_test.go:697 +0x170
  testing.tRunner()
      /snap/go/10611/src/testing/testing.go:1689 +0x180
  testing.(*T).Run.gowrap1()
      /snap/go/10611/src/testing/testing.go:1742 +0x40
==================
==================
WARNING: DATA RACE
Write at 0x00c0000460b0 by goroutine 7:
  github.com/muka/go-bluetooth/bluez/profile/adapter.(*Adapter1).OnDeviceDiscovered.func2()
      /home/rkanchan/go/pkg/mod/github.com/muka/go-bluetooth@v0.0.0-20240115085408-dfdf79b8f61d/bluez/profile/adapter/adapter_discovery.go:101 +0x64
  vstcl/device.TestOnDiscoverDataRace.func1()
      /home/rkanchan/src/call-light/src/go-sdk/lib/device/device_test.go:707 +0xa0
  runtime.deferreturn()
      /snap/go/10611/src/runtime/panic.go:602 +0x5c
  testing.tRunner()
      /snap/go/10611/src/testing/testing.go:1689 +0x180
  testing.(*T).Run.gowrap1()
      /snap/go/10611/src/testing/testing.go:1742 +0x40

Previous read at 0x00c0000460b0 by goroutine 19:
  github.com/muka/go-bluetooth/bluez/profile/adapter.(*Adapter1).OnDeviceDiscovered.func1()
      /home/rkanchan/go/pkg/mod/github.com/muka/go-bluetooth@v0.0.0-20240115085408-dfdf79b8f61d/bluez/profile/adapter/adapter_discovery.go:85 +0x2cc

Goroutine 7 (running) created at:
  testing.(*T).Run()
      /snap/go/10611/src/testing/testing.go:1742 +0x5e4
  testing.runTests.func1()
      /snap/go/10611/src/testing/testing.go:2161 +0x80
  testing.tRunner()
      /snap/go/10611/src/testing/testing.go:1689 +0x180
  testing.runTests()
      /snap/go/10611/src/testing/testing.go:2159 +0x6e0
  testing.(*M).Run()
      /snap/go/10611/src/testing/testing.go:2027 +0xb74
  main.main()
      _testmain.go:69 +0x294

Goroutine 19 (running) created at:
  github.com/muka/go-bluetooth/bluez/profile/adapter.(*Adapter1).OnDeviceDiscovered()
      /home/rkanchan/go/pkg/mod/github.com/muka/go-bluetooth@v0.0.0-20240115085408-dfdf79b8f61d/bluez/profile/adapter/adapter_discovery.go:37 +0x134
  vstcl/device.TestOnDiscoverDataRace()
      /home/rkanchan/src/call-light/src/go-sdk/lib/device/device_test.go:697 +0x170
  testing.tRunner()
      /snap/go/10611/src/testing/testing.go:1689 +0x180
  testing.(*T).Run.gowrap1()
      /snap/go/10611/src/testing/testing.go:1742 +0x40
==================
    /home/rkanchan/src/call-light/src/go-sdk/lib/device/testing.go:1398: race detected during execution of test
--- FAIL: TestOnDiscoverDataRace (0.15s)
FAIL
FAIL    vstcl/device    0.161s