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

Fix several goroutine leaks #262

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gap_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
// were connected between the two calls the signal wouldn't be picked up.
signal := make(chan *dbus.Signal)
a.bus.Signal(signal)
defer close(signal)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this close needed?
Note that the only thing closing a channel does is signal to receivers that the channel is closed (and makes sending on a channel panic). It is not needed to recover resources: a channel will be garbage collected like any other object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for this close is to end the range loop inside the goroutine and let it exit

defer a.bus.RemoveSignal(signal)
propertiesChangedMatchOptions := []dbus.MatchOption{dbus.WithMatchInterface("org.freedesktop.DBus.Properties")}
a.bus.AddMatchSignal(propertiesChangedMatchOptions...)
Expand Down
20 changes: 17 additions & 3 deletions gattc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package bluetooth

import (
"errors"
"path"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -242,6 +243,9 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
return errDupNotif
}

// Figure out the path of the device that this characteristic belongs to
devicePath := dbus.ObjectPath(path.Dir(path.Dir(string(c.characteristic.Path()))))

// Start watching for changes in the Value property.
c.property = make(chan *dbus.Signal)
c.adapter.bus.Signal(c.property)
Expand All @@ -257,12 +261,21 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
for sig := range c.property {
if sig.Name == "org.freedesktop.DBus.Properties.PropertiesChanged" {
interfaceName := sig.Body[0].(string)
if interfaceName != "org.bluez.GattCharacteristic1" {

Elara6331 marked this conversation as resolved.
Show resolved Hide resolved
switch {
case interfaceName == "org.bluez.Device1" && sig.Path == devicePath:
changes := sig.Body[1].(map[string]dbus.Variant)

if connected, ok := changes["Connected"].Value().(bool); ok && !connected {
c.EnableNotifications(nil)
return
}
case interfaceName != "org.bluez.GattCharacteristic1":
continue
}
if sig.Path != c.characteristic.Path() {
case sig.Path != c.characteristic.Path():
continue
}

changes := sig.Body[1].(map[string]dbus.Variant)
if value, ok := changes["Value"].Value().([]byte); ok {
callback(value)
Expand All @@ -280,6 +293,7 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err

err := c.adapter.bus.RemoveMatchSignal(c.propertiesChangedMatchOption)
c.adapter.bus.RemoveSignal(c.property)
close(c.property)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This close also exits the goroutine

c.property = nil
return err
}
Expand Down
Loading