BG95-M3 Callback function variable get/set issue

I’m experiencing an issue where I attempt to set a flag inside a callback function, but it’s not being set properly. Is there a specific way to correctly use flags inside callback functions?

This is my sample code:

bool flag = false;
void mqtt_client_connect_callback(qapi_Net_MQTT_Hndl_t mqtt, int reason) {
        if (!flag) my_function();
        flag = true;

The callback mentioned above is invoked multiple times, and my_function() is called only some of the times, not every time. I’ve tried making the flag “static”, “volatile”, and “atomic”, and I’ve also attempted to use it as an int data type. The same issue is occurring inside the uart_rx_cb function as well.

Is there any way to retrieve mqtt state in this: enum QAPI_NET_MQTT_CONN_STATE
I can only see the enum but there is no function specified to retrieve it.

Thanks in advance.