Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicating data stream from a device (server) after disconnecting and reconnecting the same device in Linux OS. #311

Open
raifrg opened this issue Dec 15, 2024 · 0 comments

Comments

@raifrg
Copy link

raifrg commented Dec 15, 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:

  1. 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.
  2. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant