-
Notifications
You must be signed in to change notification settings - Fork 102
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
MQTT Connection Status Thread Safety #305
MQTT Connection Status Thread Safety #305
Conversation
* #MQTTAlreadyConnected if the MQTT connection is established with the broker. | ||
* #MQTTDisconnected otherwise | ||
* | ||
* <b>Example</b> |
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 think we dont need example for this case or do you plan to addone?
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 yet to write an example for this. If we do not need it then I can skip it. Please let me know.
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.
Thank you for this Pull Request @DakshitBabbar!
I have left a few comments. Can you please take a look?
Also an overarching comment. We seem to be holding the mutex for a lot longer. If not absolutely necessary, please relinquish the mutex and take it back again when executing a critical section. This allows other threads to take the mutex in between the giving and taking of the mutex by the current task (think of a high priority task waiting on the mutex). In such cases, priority inversion happens, but lets try and keep it as short a duration as possible.
Thanks again!
source/core_mqtt.c
Outdated
connectStatus=pContext->connectStatus; | ||
|
||
if( connectStatus != MQTTNotConnected) | ||
{ | ||
status = (connectStatus==MQTTConnected) ? MQTTStatusConnected: MQTTStatusDisconnectPending; | ||
} |
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.
Why can we not do this check even before calling MQTT_GetConnectPacketSize
?
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.
Reasoning I applied:
If the check failed then it would be good that we will exit early, but if it passed then the Mutex will be held for a longer period of time, unnecessarily.
But yes one thing can be done:
Take the mutex, do the check in the beginning, if fails, change status and bubble it up, else relinquish the mutex and take it back when needed again. Is this what we want to do?
EDIT: We should not relinquish the mutex after checking the state as the state might be changed by some other thread and when this thread continues it might assume that the state is still connected and try to send the packet. Hence if we put this check before MQTT_GetConnectPacketSize
then the mutexes will contain unnecessary code, which we do not want. Therefore I think this is a good approach.
please let me know otherwise. Thanks!
source/core_mqtt.c
Outdated
/* mutex should be released and not before updating the state | ||
* because we need to make sure that the state is updated | ||
* after sending the publish packet, before the receive | ||
* loop receives ack for this and would want to update its state | ||
*/ | ||
if( stateUpdateHookExecuted == true ) |
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 realize that this was already there, but why not fix it when we are modifying the library for the better :)
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.
fixed
/bot run formatting |
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.
Few minor comments. Thank you for your patience @DakshitBabbar
--output-file=${CMAKE_BINARY_DIR}/base_coverage.info | ||
--include "*source*" |
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.
Curious why this change was required. Not a blocker, I can approve with or without this change but just curious.
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.
This was required because while building unit tests we were having some errors on mac. So @aggarg suggested this change along with many more to resolve them.
/bot run formatting |
Description
Following is a brief summary of changes:
Following are the specifics of the changes:
a. sendPublishAcks
b. MQTT_Connect
c. MQTT_Subscribe
d. MQTT_Publish
e. MQTT_Ping
f. MQTT_Unsubscribe
g. MQTT_Disconnect
const
keyword for the the MQTTStatus_t is removed in the input parameters for the receive functions as we need to update the connection status when the receive function returns a negative error codeRelevant Explanations
Pending Tasks