From 30c0522ebbcb94040819ae27abc37463eec54697 Mon Sep 17 00:00:00 2001 From: Pete Schwamb Date: Fri, 2 Oct 2026 01:42:04 -0500 Subject: [PATCH] Drop queued packets from a previous connection on reconnect When the phone reconnects, packets still queued in cmdInput, dataInput and messageInput from the old connection were consumed by the session negotiated on the new one. In one case a stale encrypted command was read as the EAP-AKA Success, which failed to parse ("invalid eap code: 43") and killed the simulator with log.Fatalf. Drain those queues in the CentralConnected handler, before the new connection's first command is read. --- pkg/bluetooth/bluetooth.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/bluetooth/bluetooth.go b/pkg/bluetooth/bluetooth.go index 17e7af8..310570d 100644 --- a/pkg/bluetooth/bluetooth.go +++ b/pkg/bluetooth/bluetooth.go @@ -90,6 +90,7 @@ func New(adapterID string, podId []byte) (*Ble, error) { b.genMtx.Lock() b.connGen++ b.genMtx.Unlock() + b.drainInputs() // Wake any in-flight CommandLoop so it can tear down and // re-establish a session on this new connection. select { @@ -312,6 +313,21 @@ func (b *Ble) ReadMessageWithTimeout(d time.Duration) (*message.Message, bool, b } } +// drainInputs discards packets and messages still queued from a previous +// connection, so the session negotiated on a new connection can't consume +// them (e.g. a stale encrypted command read as the EAP-AKA Success). +func (b *Ble) drainInputs() { + for { + select { + case <-b.cmdInput: + case <-b.dataInput: + case <-b.messageInput: + default: + return + } + } +} + func (b *Ble) servingCurrentConnection() bool { b.genMtx.Lock() defer b.genMtx.Unlock()