IotMqtt_Disconnect flags

In case a MQTT disconnectCallback is being called as of an external event (lost of network for example) I’m using a retry timer to initiate a reconnect retries, before I do that I’m calling a cleanup function that tries to call IotMqtt_Disconnect with a “cleanup only” flag (the second parameter of IotMqtt_Disconnect ).
The reason to do it is to mask the receiving of another call to the disconnectCallback that mess up my disconnect logic.
But, I can see that the disconnectCallback is being called twice, the first time is from the actual disconnect event but the second time is after I call IotMqtt_Disconnect with the IOT_MQTT_FLAG_CLEANUP_ONLY.
Can you please suggest what I’m doing wrong here?

Hello Eyal,

Could you expand on what version of the source code you are using? Did you download it from freertos.org, or are you using Amazon FreeRTOS?

Sorry for that, I’m using 201906 from AWS AFR

Hello Eyal,

I apologize for the delay. The disconnect callback gets invoked even when IOT_MQTT_FLAG_CLEANUP_ONLY is set.

“The reason to do it is to mask the receiving of another call to the disconnectCallback that mess up my disconnect logic.”
Can you expand upon this a bit?

Hi Carl,
I’m also sorry for the late response.
The issue I see with the fact that disconnectCallback is being called as a side effect when calling IotMqtt_Disconnect with the IOT_MQTT_FLAG_CLEANUP_ONLY set is that if I’m in a situation where I got a MQTT disconnect event from the outside (e.g. network cutoff) that trigger the callback, I now want to reconnect after I clean up the previous connection by calling IotMqtt_Disconnect with the IOT_MQTT_FLAG_CLEANUP_ONLY and I find my self again in the disconnect callback and I need to make some logic to distinguish between the first time it was called (from an outside event) and the second time that was caused by me cleaning up.
I thought that the flag, as its name stated, only cleanup and should not trigger the callback.
I hope I’m clear.
Thanks