Skip to content

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

@raifrg

Description

@raifrg

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions