Skip to content

Commit 4557b5d

Browse files
committed
Fix goroutine leaks
1 parent 12b6f0b commit 4557b5d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

gap_linux.go

+1
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
345345
// were connected between the two calls the signal wouldn't be picked up.
346346
signal := make(chan *dbus.Signal)
347347
a.bus.Signal(signal)
348+
defer close(signal)
348349
defer a.bus.RemoveSignal(signal)
349350
propertiesChangedMatchOptions := []dbus.MatchOption{dbus.WithMatchInterface("org.freedesktop.DBus.Properties")}
350351
a.bus.AddMatchSignal(propertiesChangedMatchOptions...)

gattc_linux.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package bluetooth
44

55
import (
66
"errors"
7+
"path"
78
"sort"
89
"strings"
910
"time"
@@ -242,6 +243,9 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
242243
return errDupNotif
243244
}
244245

246+
// Figure out the path of the device that this characteristic belongs to
247+
devicePath := dbus.ObjectPath(path.Dir(path.Dir(string(c.characteristic.Path()))))
248+
245249
// Start watching for changes in the Value property.
246250
c.property = make(chan *dbus.Signal)
247251
c.adapter.bus.Signal(c.property)
@@ -257,12 +261,22 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
257261
for sig := range c.property {
258262
if sig.Name == "org.freedesktop.DBus.Properties.PropertiesChanged" {
259263
interfaceName := sig.Body[0].(string)
260-
if interfaceName != "org.bluez.GattCharacteristic1" {
264+
265+
if interfaceName == "org.bluez.Device1" && sig.Path == devicePath {
266+
changes := sig.Body[1].(map[string]dbus.Variant)
267+
268+
if connected, ok := changes["Connected"].Value().(bool); ok && !connected {
269+
c.EnableNotifications(nil)
270+
return
271+
}
272+
} else if interfaceName != "org.bluez.GattCharacteristic1" {
261273
continue
262274
}
275+
263276
if sig.Path != c.characteristic.Path() {
264277
continue
265278
}
279+
266280
changes := sig.Body[1].(map[string]dbus.Variant)
267281
if value, ok := changes["Value"].Value().([]byte); ok {
268282
callback(value)
@@ -280,6 +294,7 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
280294

281295
err := c.adapter.bus.RemoveMatchSignal(c.propertiesChangedMatchOption)
282296
c.adapter.bus.RemoveSignal(c.property)
297+
close(c.property)
283298
c.property = nil
284299
return err
285300
}

0 commit comments

Comments
 (0)