You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've tried to use the existing code from the release branch and realized that it behaves badly. My issue relates to Linux version of the file gattc_linux.go. The implementation of 'func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte))' method has two problems:
When we lose connection to the device, I need to disable notifications (and release resources) by calling EnableNotifications(nil), but in case of 'nil' the method returns immediately because channel c.property is always nil after commit 5746ccf which removed pointer receiver (so channel c.property is never saved in the structure). As a result, the goroutine with the callback started when notifications were enabled continues to run. Every time after reconnecting and re-enabling notifications we have one more goroutine running, duplicating the data flow from the device.
Even if this method had a pointer receiver, after calling EnableNotifications(nil) the goroutine with the callback would continue to run because it relies on the 'range' loop which is only closed if the channel c.property is closed. But when we call EnableNotifications(nil) to disable notifications, the channel is set to nil without closing it (after commit f844306).
My proposal to fix these issues is quite simple:
a) use pointer receiver for this method:
func (c *DeviceCharacteristic) EnableNotifications(callback func(buf []byte))
b) when method is called with 'nil' callback
close the channel close(c.property)
before setting it to nil c.property = nil
I cloned the repository and the code with the proposed changes executes correctly.
Certainly, we can avoid using a pointer receiver if we use another service channel to close the goroutine, for example. But in this case it is better to release channel resource after disconnect instead of use one more channel.
Probably, this issue duplicates issues #291, #260 and #252
The text was updated successfully, but these errors were encountered:
raifrg
pushed a commit
to raifrg/bluetooth
that referenced
this issue
Dec 23, 2024
I've tried to use the existing code from the release branch and realized that it behaves badly. My issue relates to Linux version of the file gattc_linux.go. The implementation of 'func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte))' method has two problems:
My proposal to fix these issues is quite simple:
a) use pointer receiver for this method:
func (c *DeviceCharacteristic) EnableNotifications(callback func(buf []byte))
b) when method is called with 'nil' callback
close the channel
close(c.property)
before setting it to nil
c.property = nil
I cloned the repository and the code with the proposed changes executes correctly.
Certainly, we can avoid using a pointer receiver if we use another service channel to close the goroutine, for example. But in this case it is better to release channel resource after disconnect instead of use one more channel.
Probably, this issue duplicates issues #291, #260 and #252
The text was updated successfully, but these errors were encountered: