I am trying to implement network reconnection logic to avoid network disconnection in runtime,
- IotMqtt_Connect() is called with disconnect callback set, so whenever the MQTT is disconnected this callback should be invoked,
- Whenever there is no network, call back is called with reason IOT_MQTT_KEEP_ALIVE_TIMEOUT,
- In a separate task, i am Waiting for connection retry delay, and calling IotMqtt_Connect() again.
When ping request is not received _IotMqtt_CloseNetworkConnection() is called which inturn calls SOCKETS_Shutdown() ultimately, not SOCKETS_Close()
so if I call IotMqtt_Connect() , it creates a new socket instead of using the existing one, my wifi(TI CC3135) supports max of 16 sockets, after retrying for 16 times, I am getting error “no free sockets available”
Thank you for bringing this to our attention; I’m looking into this.
Looking at the code of _IotMqtt_ProcessKeepAlive in
iot_mqtt_operation.c, it looks like there’s an internal call to _IotMqtt_CloseNetworkConnection, which closes the connection, but does not destroy it. In the v4_beta version of MQTT from which this code is bases, there’s a subsequent call to _IotMqtt_DecrementConnectionReferences, which ends up destroying the network connection. Adding this same call to the amazon-freertos version might solve your problem.
Additionally, as a workaround, have you tried destroying the network connection with SOCKETS_Close() yourself, in the disconnect callback? This might work for you in the meantime as you wait for a solution.
Additionally, in your disconnect callback, can you try calling
IotMqtt_Disconnect() with the flag
IOT_MQTT_FLAG_CLEANUP_ONLY, whenever the disconnect reason is anything other than
IOT_MQTT_DISCONNECT_CALLED, as described in this GitHub issue?