From c4495c35cc99955202e8e4b1aa86203d0bd479c0 Mon Sep 17 00:00:00 2001 From: Wilko Date: Tue, 3 Jun 2025 12:57:58 +0200 Subject: [PATCH 1/2] Fix Bug with connectionCtx --- internal/ble/control/control.go | 5 +++++ internal/tesla/commands/commands.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/ble/control/control.go b/internal/ble/control/control.go index f46d58c..4a2f2ad 100644 --- a/internal/ble/control/control.go +++ b/internal/ble/control/control.go @@ -371,9 +371,11 @@ func (bc *BleControl) ExecuteCommand(car *vehicle.Vehicle, command *commands.Com // Wrap ctx with connectionCtx ctx, cancel := context.WithCancel(ctx) defer cancel() + connectionCtxDone := false go func() { select { case <-connectionCtx.Done(): + connectionCtxDone = true cancel() case <-ctx.Done(): } @@ -386,6 +388,9 @@ func (bc *BleControl) ExecuteCommand(car *vehicle.Vehicle, command *commands.Com select { case <-time.After(sleep): case <-ctx.Done(): + if connectionCtxDone { + return command, ctx.Err(), ctx + } return nil, ctx.Err(), ctx } sleep *= 2 diff --git a/internal/tesla/commands/commands.go b/internal/tesla/commands/commands.go index 53d3d78..ce0a41f 100644 --- a/internal/tesla/commands/commands.go +++ b/internal/tesla/commands/commands.go @@ -166,7 +166,7 @@ func (command *Command) Send(ctx context.Context, car *vehicle.Vehicle) (shouldR log.Debugf("get: %s", endpoint) category, err := GetCategory(endpoint) if err != nil { - return false, fmt.Errorf("unrecognized state category charge") + return false, err } data, err := car.GetState(ctx, category) if err != nil { From 2f134691c1914e3b814d832ef6f2c31d23663ed1 Mon Sep 17 00:00:00 2001 From: Wilko Date: Tue, 3 Jun 2025 13:27:29 +0200 Subject: [PATCH 2/2] Update control.go --- internal/ble/control/control.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/internal/ble/control/control.go b/internal/ble/control/control.go index 4a2f2ad..8bab185 100644 --- a/internal/ble/control/control.go +++ b/internal/ble/control/control.go @@ -371,11 +371,11 @@ func (bc *BleControl) ExecuteCommand(car *vehicle.Vehicle, command *commands.Com // Wrap ctx with connectionCtx ctx, cancel := context.WithCancel(ctx) defer cancel() - connectionCtxDone := false + + // Create a single goroutine to handle both context cancellations go func() { select { case <-connectionCtx.Done(): - connectionCtxDone = true cancel() case <-ctx.Done(): } @@ -385,10 +385,11 @@ func (bc *BleControl) ExecuteCommand(car *vehicle.Vehicle, command *commands.Com if i > 0 { log.Warn(lastErr) log.Info(fmt.Sprintf("retrying in %d seconds", sleep/time.Second)) + select { case <-time.After(sleep): case <-ctx.Done(): - if connectionCtxDone { + if connectionCtx.Err() != nil { return command, ctx.Err(), ctx } return nil, ctx.Err(), ctx @@ -398,20 +399,21 @@ func (bc *BleControl) ExecuteCommand(car *vehicle.Vehicle, command *commands.Com retry, err := command.Send(ctx, car) if err == nil { - //Successful log.Info("successfully executed", "command", command.Command, "body", command.Body) return nil, nil, ctx - } else if !retry { + } + + if !retry { return nil, nil, ctx - } else { - //closed pipe - if strings.Contains(err.Error(), "closed pipe") { - //connection lost, returning the command so it can be executed again - return command, err, ctx - } - lastErr = err } + + if strings.Contains(err.Error(), "closed pipe") { + return command, err, ctx + } + + lastErr = err } + log.Error("canceled", "command", command.Command, "body", command.Body, "err", lastErr) return nil, lastErr, ctx }