paypal / gatt

Gatt is a Go package for building Bluetooth Low Energy peripherals
BSD 3-Clause "New" or "Revised" License
1.12k stars 283 forks source link

Deadlock when rapidly sending notifications #71

Open rpendleton opened 8 years ago

rpendleton commented 8 years ago

I'm new to Go, so maybe I'm just doing something wrong, but I'm running into a deadlock issue when I try to write notifications at a fairly quick rate. I know BLE has some limits of its own, but I figured something down the line would queue my notifications and send them when possible. Instead, my application is crashing.

Here's the code I'm using to send notifications:

func testConnection(r gatt.Request, n gatt.Notifier) {
    i := 0
    for !n.Done() {
        for j := 0; j < 4; j++ {
            fmt.Fprintf(n, "%d", i)
            i++
        }
        time.Sleep(20 * time.Millisecond)
    }
}

s.AddCharacteristic(gatt.MustParseUUID(serviceUUID)).HandleNotifyFunc(
    func(r gatt.Request, n gatt.Notifier) {
        go testConnection(r, n)
    })

And here's a copy of the stack trace when the deadlock occurs: gatt deadlock.log. Is this expected behavior? If so, is there any way of knowing how quickly I can send notifications? I'm assuming it varies based on device. I'm able to run the above code with a sleep of 80 milliseconds when the connection is from an iOS device.