Skip to content

Commit

Permalink
Merge pull request #1 from sethterashima/fix-blocking-scan-goroutine
Browse files Browse the repository at this point in the history
Fix MacOS deadlock during BLE scan
  • Loading branch information
Lenart12 authored Jan 23, 2025
2 parents 9ddc8ea + 9496b02 commit 01237bb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23
require (
github.com/99designs/keyring v1.2.2
github.com/cronokirby/saferith v0.33.0
github.com/go-ble/ble v0.0.0-20220207185428-60d1eecf2633
github.com/go-ble/ble v0.0.0-20240122180141-8c5522f54333
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
golang.org/x/term v0.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dvsekhvalnov/jose2go v1.6.0 h1:Y9gnSnP4qEI0+/uQkHvFXeD2PLPJeXEL+ySMEA2EjTY=
github.com/dvsekhvalnov/jose2go v1.6.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/go-ble/ble v0.0.0-20220207185428-60d1eecf2633 h1:ZrzoZQz1CF33SPHLkjRpnVuZwr9cO1lTEc4Js7SgBos=
github.com/go-ble/ble v0.0.0-20220207185428-60d1eecf2633/go.mod h1:fFJl/jD/uyILGBeD5iQ8tYHrPlJafyqCJzAyTHNJ1Uk=
github.com/go-ble/ble v0.0.0-20240122180141-8c5522f54333 h1:bQK6D51cNzMSTyAf0HtM30V2IbljHTDam7jru9JNlJA=
github.com/go-ble/ble v0.0.0-20240122180141-8c5522f54333/go.mod h1:fFJl/jD/uyILGBeD5iQ8tYHrPlJafyqCJzAyTHNJ1Uk=
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0=
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
Expand Down
25 changes: 19 additions & 6 deletions pkg/connector/ble/ble.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ble
import (
"context"
"crypto/sha1"
"errors"
"fmt"
"strings"
"sync"
Expand Down Expand Up @@ -169,16 +170,28 @@ func ScanVehicleBeacon(ctx context.Context, vin string) (Advertisement, error) {
func scanVehicleBeacon(ctx context.Context, localName string) (Advertisement, error) {
var err error
ctx2, cancel := context.WithCancel(ctx)
defer cancel()

ch := make(chan Advertisement)
ch := make(chan Advertisement, 1)
fn := func(a Advertisement) {
if a.LocalName() != localName {
return
}
cancel()
ch <- a
select {
case ch <- a:
cancel() // Notify device.Scan() that we found a match
case <-ctx2.Done():
// Another goroutine already found a matching advertisement. We need to return so that
// the MacOS implementation of device.Scan(...) unblocks.
}
}

if err = device.Scan(ctx2, false, fn); !errors.Is(err, context.Canceled) {
// If ctx rather than ctx2 was canceled, we'll pick that error up below. This is a bit
// hacky, but unfortunately device.Scan() _always_ returns an error on MacOS because it does
// not terminate until the provided context is canceled.
return nil, err
}
err = device.Scan(ctx2, false, fn)

select {
case a, ok := <-ch:
Expand All @@ -187,8 +200,8 @@ func scanVehicleBeacon(ctx context.Context, localName string) (Advertisement, er
return nil, fmt.Errorf("scan channel closed")
}
return a, nil
default:
return nil, err
case <-ctx.Done():
return nil, ctx.Err()
}
}

Expand Down

0 comments on commit 01237bb

Please sign in to comment.