bluetooth icon indicating copy to clipboard operation
bluetooth copied to clipboard

[hci] If UART data comes in too fast, we overrun read buffer

Open kendallgoto opened this issue 1 month ago • 1 comments

https://github.com/tinygo-org/bluetooth/blob/5c615298c3e4400150c44da3636f3d3b10967e3c/hci.go#L199C1-L203C1

While polling HCI, there are many sleeps that should likely be yields, if anything. However, if we're running an operation over HCI that emits a large amount of packets (such as BT scan), it is very easy to overrun the HCI buffer because of these sleeps. Pending HCI messages queue up, then when the system tries to read from the HCI transport, it will attempt to read all of the pending HCI packets at once, rather than filling its buffer.

As long as we don't receive a single HCI message that is larger than our buffer, which appears to be limited at 255 bytes in the spec, all that needs to happen is that we stop reading when the buffer is filled, rather than attempt to read beyond our means.

kendallgoto avatar Nov 27 '25 01:11 kendallgoto

@kendallgoto thanks for the report. I just created https://github.com/tinygo-org/bluetooth/pull/399 that should address this. Please take a look!

deadprogram avatar Dec 06 '25 08:12 deadprogram