Please help with MQTT and OTA startup and shutdown

Hi all

My current code looks like:

void mqttOnDisconnect()
  disconnected = true;


I pull the ethernet plug and get mqttOnDisconnect(KEEP_ALIVE_TIMEOUT) as expected.

Problem here is that by the time OTA_AgentShutdown() is called the MQTT connection is aleady closed and _IotMqtt_RemoveSubscriptionByTopicFilter() hangs on ASSERT (contextIndex = -1).

I then tried moving OTA_AgentShutdown() to mqttOnDisconnect(), but it looks as if OTA spins off a job that ends up calling _IotMqtt_RemoveSubscriptionByTopicFilter() after OTA_AgentShutdown() returns (leading to mqttOnDisconnect() returning leading to MQTT closing the connection).

So, when do I call OTA_AgentShutdown() or don’t I need to do that (i.e. does MQTT clean that up properly?)

Or do I need to wait for OTA to shutdown in mqttOnDisconnect? This feels… wrong somehow :slight_smile:




Can you please share the version/release you are using ?

Hi pvyawaha

Latest version of everything as far as I can tell.

MQTT V2.3.1
OTA 1.2.1
FreeRTOS 10.4.3

OK to close this thread gracefully. Thanks to all the people who made the patches which ended up fixing my problem.

For the record, after mqttOnDisconnect(KEEP_ALIVE_TIMEOUT) the latest code requires that I call IotMqtt_Disconnect() after calling OTA_AgentShutdown. IotMqtt_Disconnect now checks and doesn’t complain or break when called after a timeout, and frees all the resources that need to be free’ed.