-
Notifications
You must be signed in to change notification settings - Fork 611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[hal] Fix potential race in CANAPI #6819
Conversation
if (*status != 0) { | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question for @ThadHouse: will netcomm still update its internal state if it returns an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am unsure. But I do seem to remember a mention from NI that those functions basically can't fail when any of the periodic flags are set.
I've removed the status check on tx functions, making the assumption that even if the function returns a bad status, the periodic state will still have been updated. |
Needs conflicts resolved. |
40fc634
to
1a0c448
Compare
Currently, the call to
HAL_CAN_SendMessage
is not synchronized with updates toperiodicSends
(which represents the internal state of the netcomm sender thread).Now, the mutex is locked before
HAL_CAN_SendMessage
is called to ensure the update is atomic.periodicSends and receives also now have their own mutexes to reduce unnecessary contention between send and receive functions.
Ex:
Thread A calls
HAL_StopCANPacketRepeating
with apiId 0Thread B calls
HAL_WriteCANPacketRepeating
with apiId 0 and repeatMs 10Inside
HAL_StopCANPacketRepeating
, Thread A callsHAL_CAN_SendMessage
, which updates netcomm's state to not repeat the packetThread A is paused
Inside
HAL_WriteCANPacketRepeating
, Thread B callsHAL_CAN_SendMessage
, which updates netcomm's state to repeat the packetThread B locks the mutex
Thread B updates the map to indicate the new state (packet is repeating)
Thread B exits
HAL_WriteCANPacketRepeating
and unlocks the mutexThread A resumes
Thread A locks the mutex
Thread A updates the map with what it thinks the new state is (packet is not repeating)
Thread A exits
HAL_StopCANPacketRepeating
and unlocks the mutexThread A calls
HAL_CleanCAN
, which doesn't stop the repeating packet because the state has diverged.