ESP 8266 memory leaks in work with Google cloud

#1

I have a problem with free memory in work with Google Cloud platform. I have write a simple code, which every 3 min send free memory size to google cloud. But time to time MCU reconnent to server by the reason “Dropping MQTT connection due to imminent token expiration”. After that free memory size droped out to 340 byte.

  1. My goal is: long MCU work time without rebooting
  2. My code is:
static void my_timer_state(void *arg)
{
LOG(LL_INFO, ("{mem:%d,uptime:%d}", (int)mgos_get_free_heap_size(), (int)mgos_uptime())); 
 if (connected && mgos_gcp_is_connected())
  {
    if (mgos_gcp_send_event_subf("registry", "{mem:%d,uptime:%d}",
                                 (int)mgos_get_free_heap_size(), (int)mgos_uptime()))
    {
      LOG(LL_INFO, ("GCP state send"));
    }
  }
  (void)arg;
}

static void my_net_ev_handler(int ev, void *evd, void *arg)
{
  if (ev == MGOS_NET_EV_IP_ACQUIRED)
  {
    connected = true;
  }
  else
  {
    connected = false;
  }
  (void)evd;
  (void)arg;
}
//
enum mgos_app_init_result mgos_app_init(void)
{
  mgos_set_timer(mgos_sys_config_get_app_statusTimer(), MGOS_TIMER_REPEAT, my_timer_state, NULL);
  mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, my_net_ev_handler, NULL);
  return MGOS_APP_INIT_SUCCESS;
}

Full project code here: https://github.com/sergkit/testmem
3. The result I see is:

main.c:27               GCP state send
main.c:21               {mem:42388,uptime:3240}
main.c:27               GCP state send
main.c:21               {mem:42388,uptime:3420}
main.c:27               GCP state send
mgos_gcp.c:86           Dropping MQTT connection due to imminent token expiration
mgos_mqtt.c:167         MQTT Disconnect
mgos_mqtt.c:511         MQTT connecting after 2116 ms
mgos_mqtt.c:435         MQTT connecting to mqtt.googleapis.com:8883
mg_ssl_if_mbedtls.c:35  0x3fff0b54 ciphersuite: TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256
SW ECDH curve 3
mgos_mongoose.c:66      New heap free LWM: 25760
mgos_mqtt.c:141         MQTT TCP connect ok (0)
mgos_mqtt.c:185         MQTT CONNACK 0
mgos_mqtt.c:125         Subscribing to '/devices/WMETER_11A214/commands/#' (QoS 1)
main.c:21               {mem:42048,uptime:3600}
main.c:27               GCP state send
main.c:21               {mem:42048,uptime:3780}
main.c:27               GCP state send

The same result I see after SNTP Query

main.c:21               {mem:41544,uptime:7380}
main.c:27               GCP state send
main.c:21               {mem:41544,uptime:7560}
main.c:27               GCP state send
mgos_sntp.c:95          SNTP query to time.google.com
main.c:21               {mem:41320,uptime:7740}
main.c:27               GCP state send
main.c:21               {mem:41320,uptime:7920}
  1. My expectation & question is: I expect, what memory should not leaks in this code. How to do it?
#2

here’s what’s going on.
the source of the leak is ctx.jwt mbuf created here - it is never freed. it is done on purpose, the comment here explicitly says so.
however, it is wrong. mqtt auth callback was refactored in this commit and it changed ownership of the returned credentials: previously they would be freed by the function, now caller retains ownership. however, this commit just carried over the line that pass is not freed, but in fact now it should be. this commit fixes it.

thank you for a detailed report!

#3

Thank you.
And what about memory leak in the SNTP query?

#4

i don’t think we have a leak in SNTP. in your log you have query send event but the memory should be released once a response is received.