coreMQTT-Agent icon indicating copy to clipboard operation
coreMQTT-Agent copied to clipboard

operations fail if arguments go out of scope

Open marcusb opened this issue 11 months ago • 3 comments

The MQTTAgent_Publish() method requires that the pPublishInfo argument (carrying the topic name) remains in scope after the call returns. This is not documented in the API doc, which only says:

The context passed to the callback through pCmdContext member of pCommandInfo parameter MUST remain in scope at least until the callback has been executed by the agent task.

I am allocating the parameters to the Publish call on the stack, and do not want to wait for an ack, just send a fire-and-forget request. I can do this by setting the callback pointer to NULL. But that still doesn't work, because the library retains the pPublishInfo pointer. But that memory is deallocated when my method returns, leading to undefined behavior.

Perhaps the library should make a copy of the supplied MQTTPublishInfo_t instead.

marcusb avatar Dec 26 '24 18:12 marcusb

Hi @marcusb, I will take a deeper look at this. Thank you for bringing it to attention! Best, Jason Carroll

jasonpcarroll avatar Dec 26 '24 23:12 jasonpcarroll

Hello @marcusb, Increasing the priority of the mqtt task can help out with solving this issue. We will also update the doc to reflect this requirement. Please let us know if you have more questions

rawalexe avatar Jan 07 '25 03:01 rawalexe

Hello @marcusb,

Firstly, apologies for the delay on our part.

Secondly, I see your point. Thank you for reporting this issue. We will update the documentation. But the behavior of the library cannot be easily changed.

For your application to work, as @rawalexe mentioned above, can you raise the priority of the coreMQTT-Agent? That will cause the agent to wake up as soon as your task adds any data to the agent queue and process it. This will cause your task to be preempted and sleep while the Agent uses the passed arguments to send publish packet. When it is done, your task will wake up and it will be okay to free the memory used by the arguments.

Let us know if this works.

Thanks, Aniruddha

AniruddhaKanhere avatar Jan 07 '25 23:01 AniruddhaKanhere

@marcusb I am closing this issue due to lack of response. Please feel free to open/reopen issue if you have any more questions and/or feedback

AniruddhaKanhere avatar Mar 28 '25 21:03 AniruddhaKanhere

Hi, confirming that raising the priority of the agent task solves the problem. Thank you!

marcusb avatar Apr 03 '25 03:04 marcusb

I observed this as well. Shouldn't this issue stay open at least until a PR is merged fixing the documentation?

MastermindPanda avatar Apr 24 '25 12:04 MastermindPanda