Skip to content

Fix lost packet when RxDone fires during recvRaw() - #3999

Open
magnusesp wants to merge 1 commit into
meshcore-dev:devfrom
magnusesp:fix/recvraw-rx-race
Open

magnusesp wants to merge 1 commit into
meshcore-dev:devfrom
magnusesp:fix/recvraw-rx-race

Conversation

@magnusesp

Copy link
Copy Markdown

recvRaw() reads the volatile state twice. If the DIO1 interrupt (setFlag) sets STATE_INT_READY after the first test and before the second, the second test sees STATE_RX | STATE_INT_READY, so startReceive() runs: it clears the radio's IRQ status, discarding the received packet unread, and the following state = STATE_RX drops the flag. Mask the flag in the second test, as isInRecvMode() already does, so the packet is read on the next call.

Found while running a room server build in an emulator: injecting a packet while the CPU was between the two reads lost it every time, and with this change it was received.

Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com

recvRaw() reads the volatile state twice. If the DIO1 interrupt (setFlag)
sets STATE_INT_READY after the first test and before the second, the second
test sees STATE_RX | STATE_INT_READY, so startReceive() runs: it clears the
radio's IRQ status, discarding the received packet unread, and the
following state = STATE_RX drops the flag. Mask the flag in the second test,
as isInRecvMode() already does, so the packet is read on the next call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@magnusesp
magnusesp changed the base branch from main to dev November 29, 2026 11:14
@MarkRose

Copy link
Copy Markdown

I traced this on current dev. If DIO1 fires between the state & STATE_INT_READY test and the state != STATE_RX test, state is 17, so startReceive() runs. In RadioLib 7.8.1 that does standby + setBufferBaseAddress + clearIrqStatus, and then state = STATE_RX drops the flag, so the packet is lost. With the mask, 17 compares as RX, startReceive is skipped and the packet is read on the next loop. All other state values (IDLE/TX_WAIT/TX_DONE, with or without the flag) take the same path as before, and for LR2021 it also avoids a startReceive while still in RX. Heltec_v3_repeater and RAK_4631_repeater build fine.

Reviewed with Claude (AI); I checked the findings before posting.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants