Skip to content

Commit

Permalink
fix cleanup of message listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
solipsis committed Jan 11, 2019
1 parent 0f81f6e commit 506c69b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pkg/keepkey/keepkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Keepkey struct {
label, serial, id string // Used for specifying which device to send commands if multiple are connected
logger
deviceQueue, debugQueue chan *deviceResponse // for subscribing to responses over different interfaces
closed chan struct{}
}

// transport contains handles to the primary and debug interfaces of the target device
Expand Down Expand Up @@ -93,13 +94,15 @@ func newKeepkeyFromConfig(cfg *Config) *Keepkey {
kk.autoButton = cfg.AutoButton
kk.deviceQueue = make(chan *deviceResponse, 1)
kk.debugQueue = make(chan *deviceResponse, 1)
kk.closed = make(chan struct{}, 1)

return kk
}

// Close closes the transport connection and unassoctiates that nterface
// with the calling Keepkey
func (kk *Keepkey) Close() {
close(kk.closed) // signal message listeners to stop
if kk.transport.conn != nil {
kk.transport.conn.Close()
kk.transport.conn = nil
Expand Down
17 changes: 12 additions & 5 deletions pkg/keepkey/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func GetDevicesWithConfig(cfg *Config) ([]*Keepkey, error) {
kk := newKeepkeyFromConfig(cfg)
kk.transport = dev
if dev.debug != nil {
go listenForMessages(kk.transport.debug, kk.debugQueue)
go listenForMessages(kk.transport.debug, kk.debugQueue, kk.closed)
fmt.Println("DebugLink established over WebUSB")
}
go listenForMessages(kk.transport.conn, kk.deviceQueue)
go listenForMessages(kk.transport.conn, kk.deviceQueue, kk.closed)

// Ping the device and ask for its features
features, err := kk.Initialize()
Expand Down Expand Up @@ -96,7 +96,7 @@ func GetDevicesWithConfig(cfg *Config) ([]*Keepkey, error) {
continue
}
kk.transport.conn = device
go listenForMessages(device, kk.deviceQueue)
go listenForMessages(device, kk.deviceQueue, kk.closed)

// debug HID interface
if debugIFace.Path != "" {
Expand All @@ -107,7 +107,7 @@ func GetDevicesWithConfig(cfg *Config) ([]*Keepkey, error) {
}
fmt.Println("Debug link established over HID")
kk.transport.debug = debug
go listenForMessages(debug, kk.debugQueue)
go listenForMessages(debug, kk.debugQueue, kk.closed)
}

// Ping the device and ask for its features
Expand All @@ -132,7 +132,7 @@ func GetDevicesWithConfig(cfg *Config) ([]*Keepkey, error) {
}

// passively listen for messages on a transport interface
func listenForMessages(in io.Reader, out chan *deviceResponse) {
func listenForMessages(in io.Reader, out chan *deviceResponse, closed chan struct{}) {
for {
// stream the reply back in 64 byte chunks
chunk := make([]byte, 64)
Expand All @@ -141,6 +141,13 @@ func listenForMessages(in io.Reader, out chan *deviceResponse) {
for {
// Read next chunk
if _, err := io.ReadFull(in, chunk); err != nil {

// Abort if this keepkey has closed its connections
select {
case <-closed:
return
default:
}
fmt.Println(color.RedString("Unable to read chunk from device:", err)) // TODO: move to device specific log
break
}
Expand Down

0 comments on commit 506c69b

Please sign in to comment.