Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in esp_mqtt_destroy_config() (IDFGH-2293) #4433

Closed
eespi opened this issue Dec 3, 2019 · 3 comments
Closed

Memory leak in esp_mqtt_destroy_config() (IDFGH-2293) #4433

eespi opened this issue Dec 3, 2019 · 3 comments
Assignees

Comments

@eespi
Copy link

eespi commented Dec 3, 2019

  • Module or chip used: ESP32-WROOM-32D
  • IDF version: v4.0-beta1-279-g0a03a55
  • Build System: Make
  • Compiler version: 8.2.0
  • Operating System: Linux
  • Power Supply: external 3.3V

Problem Description

Calling esp_mqtt_destroy_client leaks about 216 bytes of memory because even loop is not freed.

The leak is located inside esp_mqtt_destroy_config which should call esp_event_loop_delete but a is memset clears the handle before it is freed.

After some time of internet outage, the application will stuck becuse no memory available and error mbedtls ssl setup returned -0x7f00

Expected Behavior

esp_event_loop_delete should be called before memset, or vice-versa

Actual Behavior

Steps to repropduce

  1. call esp_mqtt_client_init
  2. call esp_mqtt_client_destroy
  3. loop

Code to reproduce this issue

In file mqtt_client.c

static esp_err_t esp_mqtt_destroy_config(esp_mqtt_client_handle_t client)
{
    mqtt_config_storage_t *cfg = client->config;
    free(cfg->host);
    free(cfg->uri);
    free(cfg->path);
    free(cfg->scheme);
    memset(cfg, 0, sizeof(mqtt_config_storage_t));  
    !!!!!!!!!!!!!!!!!!!! NOTE client->config->event_loop_handle now is NULL !!!!!!!!!!!!!!!!!!!! 
    free(client->connect_info.will_topic);
    free(client->connect_info.will_message);
    free(client->connect_info.client_id);
    free(client->connect_info.username);
    free(client->connect_info.password);
    memset(&client->connect_info, 0, sizeof(mqtt_connect_info_t));
#ifdef MQTT_SUPPORTED_FEATURE_EVENT_LOOP
    !!!!!!!!!!!!!!!!!!!!  NOTE This never gets called !!!!!!!!!!!!!!!!!!!! 
    if (client->config->event_loop_handle) { 
        esp_event_loop_delete(client->config->event_loop_handle);
    }
#endif
    free(client->config);
    return ESP_OK;
}

This fixes the problem, allowing esp_event_loop_delete to be called:

static esp_err_t esp_mqtt_destroy_config(esp_mqtt_client_handle_t client)
{
    mqtt_config_storage_t *cfg = client->config;
    free(cfg->host);
    free(cfg->uri);
    free(cfg->path);
    free(cfg->scheme);
    free(client->connect_info.will_topic);
    free(client->connect_info.will_message);
    free(client->connect_info.client_id);
    free(client->connect_info.username);
    free(client->connect_info.password);
    memset(&client->connect_info, 0, sizeof(mqtt_connect_info_t));
#ifdef MQTT_SUPPORTED_FEATURE_EVENT_LOOP
    if (client->config->event_loop_handle) {
        esp_event_loop_delete(client->config->event_loop_handle);
    }
#endif
    memset(cfg, 0, sizeof(mqtt_config_storage_t));
    free(client->config);
    return ESP_OK;
}

Debug Logs

This is the log of heap trace recordered from init to destroy

3 allocations trace (100 entry buffer)
24 bytes (@ 0x3ffd8d30) allocated CPU 0 ccount 0xd7ac8400 caller 0x4018380e:0x4014c5db
0x4018380e: esp_event_loop_create at /media/paolo/Data/working/FW/esp32/esp-idf/components/esp_event/esp_event.c:418

0x4014c5db: esp_mqtt_client_init at /media/paolo/Data/working/FW/esp32/esp-idf/components/mqtt/esp-mqtt/mqtt_client.c:391

104 bytes (@ 0x3ffd8d4c) allocated CPU 0 ccount 0xd7ac8cb0 caller 0x4008e63a:0x4018383e
0x4008e63a: xQueueGenericCreate at /media/paolo/Data/working/FW/esp32/esp-idf/components/freertos/queue.c:392 (discriminator 2)

0x4018383e: esp_event_loop_create at /media/paolo/Data/working/FW/esp32/esp-idf/components/esp_event/esp_event.c:424

88 bytes (@ 0x3ffd8db8) allocated CPU 0 ccount 0xd7ac94f4 caller 0x4008e63a:0x4008e81b
0x4008e63a: xQueueGenericCreate at /media/paolo/Data/working/FW/esp32/esp-idf/components/freertos/queue.c:392 (discriminator 2)

0x4008e81b: xQueueCreateMutex at /media/paolo/Data/working/FW/esp32/esp-idf/components/freertos/queue.c:499

216 bytes 'leaked' in trace (3 allocations)
total allocations 27 total frees 24

This is the log of heap trace recordered after the fix:

0 allocations trace (100 entry buffer)
0 bytes 'leaked' in trace (0 allocations)
total allocations 27 total frees 27
Free Heap Size 2: 168372
@github-actions github-actions bot changed the title Memory leak in esp_mqtt_destroy_config() Memory leak in esp_mqtt_destroy_config() (IDFGH-2293) Dec 3, 2019
@ESP-Marius
Copy link
Collaborator

Yeah, this looks like an oversight. We will fix this and close the issue when it has been deployed to Github.

Thank you for reporting this.

david-cermak pushed a commit to espressif/esp-mqtt that referenced this issue Dec 4, 2019
The event loop would never get deleted due to the event loop handle being
cleared to 0 before checking if it exists.

Closes espressif/esp-idf#4433
Closes IDFGH-2293
@AxelLin
Copy link
Contributor

AxelLin commented Jan 6, 2020

Hi @david-cermak
The fix has been in esp-mqtt tree for a month but not yet in esp-idf.
I'm wondering if it is normal taking such long time to merge the fix into esp-idf
especially when the fix is so trivial.

@ESP-Marius
Copy link
Collaborator

@AxelLin Hi,
Yeah, sorry for being slow about getting it into esp-idf, I'll push a merge request for it today.

Sorry for keeping you waiting.

espressif-bot pushed a commit that referenced this issue Jan 23, 2020
Adds bugfixes for:
 - Too early publishing
 - Potential mutex memory leak
 - CI related issues.
 - Wait for entire connack message
 - Event loop not getting cleaned up

Adds support for ALPN, configurable reconnect time, QEMU CI tests and password
protected client key.

MQTT MR: esp-mqtt/merge_requests/46
Closes IDF-1162
Closes espressif/esp-mqtt#137

MQTT MR: esp-mqtt/merge_requests/47
Closes IDF-1126

MQTT MR: esp-mqtt/merge_requests/48
Closes IDFGH-2197
Closes #4349
Closes espressif/esp-mqtt#140

MQTT MR: esp-mqtt/merge_requests/48
Closes IDFGH-2235
Closes #4384

MQTT MR: esp-mqtt/merge_requests/49
Closes #4433
Closes IDFGH-2293

MQTT MR: esp-mqtt/merge_requests/50
Closes FCS-254

MQTT MR: esp-mqtt/merge_requests/53
Closes FCS-267
espressif-bot pushed a commit that referenced this issue Jan 24, 2020
Adds bugfixes for:
 - Too early publishing
 - Potential mutex memory leak
 - CI related issues.
 - Wait for entire connack message
 - Event loop not getting cleaned up

Adds support for ALPN, configurable reconnect time, QEMU CI tests and password
protected client key.

MQTT MR: esp-mqtt/merge_requests/46
Closes IDF-1162
Closes espressif/esp-mqtt#137

MQTT MR: esp-mqtt/merge_requests/47
Closes IDF-1126

MQTT MR: esp-mqtt/merge_requests/48
Closes IDFGH-2197
Closes #4349
Closes espressif/esp-mqtt#140

MQTT MR: esp-mqtt/merge_requests/48
Closes IDFGH-2235
Closes #4384

MQTT MR: esp-mqtt/merge_requests/49
Closes #4433
Closes IDFGH-2293

MQTT MR: esp-mqtt/merge_requests/50
Closes FCS-254

MQTT MR: esp-mqtt/merge_requests/53
Closes FCS-267
espressif-bot pushed a commit that referenced this issue Feb 21, 2020
Adds bugfixes for:
 - Too early publishing
 - Potential mutex memory leak
 - CI related issues.
 - Wait for entire connack message
 - Event loop not getting cleaned up

Adds support for ALPN, configurable reconnect time, QEMU CI tests and password
protected client key.

MQTT MR: esp-mqtt/merge_requests/46
Closes IDF-1162
Closes espressif/esp-mqtt#137

MQTT MR: esp-mqtt/merge_requests/47
Closes IDF-1126

MQTT MR: esp-mqtt/merge_requests/48
Closes IDFGH-2197
Closes #4349
Closes espressif/esp-mqtt#140

MQTT MR: esp-mqtt/merge_requests/48
Closes IDFGH-2235
Closes #4384

MQTT MR: esp-mqtt/merge_requests/49
Closes #4433
Closes IDFGH-2293

MQTT MR: esp-mqtt/merge_requests/50
Closes FCS-254

MQTT MR: esp-mqtt/merge_requests/53
Closes FCS-267
david-cermak pushed a commit to espressif/esp-mqtt that referenced this issue Dec 16, 2022
Adds bugfixes for:
 - Too early publishing
 - Potential mutex memory leak
 - CI related issues.
 - Wait for entire connack message
 - Event loop not getting cleaned up

Adds support for ALPN, configurable reconnect time, QEMU CI tests and password
protected client key.

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/46
Closes IDF-1162
Closes #137

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/47
Closes IDF-1126

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/48
Closes IDFGH-2197
Closes espressif/esp-idf#4349
Closes #140

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/48
Closes IDFGH-2235
Closes espressif/esp-idf#4384

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/49
Closes espressif/esp-idf#4433
Closes IDFGH-2293

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/50
Closes FCS-254

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/53
Closes FCS-267
egnor pushed a commit to egnor/esp-mqtt that referenced this issue Dec 23, 2022
Adds bugfixes for:
 - Too early publishing
 - Potential mutex memory leak
 - CI related issues.
 - Wait for entire connack message
 - Event loop not getting cleaned up

Adds support for ALPN, configurable reconnect time, QEMU CI tests and password
protected client key.

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/46
Closes IDF-1162
Closes espressif#137

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/47
Closes IDF-1126

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/48
Closes IDFGH-2197
Closes espressif/esp-idf#4349
Closes espressif#140

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/48
Closes IDFGH-2235
Closes espressif/esp-idf#4384

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/49
Closes espressif/esp-idf#4433
Closes IDFGH-2293

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/50
Closes FCS-254

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/53
Closes FCS-267
egnor pushed a commit to egnor/esp-mqtt that referenced this issue Dec 23, 2022
Adds bugfixes for:
 - Too early publishing
 - Potential mutex memory leak
 - CI related issues.
 - Wait for entire connack message
 - Event loop not getting cleaned up

Adds support for ALPN, configurable reconnect time, QEMU CI tests and password
protected client key.

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/46
Closes IDF-1162
Closes espressif#137

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/47
Closes IDF-1126

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/48
Closes IDFGH-2197
Closes espressif/esp-idf#4349
Closes espressif#140

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/48
Closes IDFGH-2235
Closes espressif/esp-idf#4384

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/49
Closes espressif/esp-idf#4433
Closes IDFGH-2293

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/50
Closes FCS-254

MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/53
Closes FCS-267
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants