HTTP server fails after a while.

hououin_kyouma
Posts: 7
Joined: Fri Nov 03, 2023 9:56 pm

HTTP server fails after a while.

Postby hououin_kyouma » Sat Nov 18, 2023 7:33 pm

Hello I'm trying to use GET method with Telegram api.

Code: Select all

static esp_err_t getUpdates_test (void)
{
    char output_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};   // Buffer to store response of http request
    // int content_length = 0;
    char _msg_update_id_append[16] = "";
    char url[512] = "";

    /* Creating the string of the url*/
    //Copy the url+TOKEN
    strcat(url, url_string);
    strcat(url, "/getUpdates?limit=1&offset=");
    sprintf(_msg_update_id_append, "%d", _msg_update_id);
    strcat(url, _msg_update_id_append);
    
    ESP_LOGI(TAG3, "url string es: %s", url);

    esp_http_client_config_t config = {
        .url = url,
        .transport_type = HTTP_TRANSPORT_OVER_SSL,
        .event_handler = _http_event_handler,
        .cert_pem = telegram_certificate_pem_start,
        .method = HTTP_METHOD_GET,
    };

    ESP_LOGW(TAG3, "Iniciare");

    esp_http_client_handle_t client = esp_http_client_init(&config);

    ESP_LOGW(TAG3, "Enviare un mensaje a un chat");
    ESP_LOGW(TAG3, "Open");

    esp_err_t err = esp_http_client_open(client, 0);
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
        esp_http_client_cleanup(client);
    }

    ESP_LOGI(TAG, "client connection open");

    int64_t content_length = esp_http_client_fetch_headers (client);
    ESP_LOGI(TAG, "content_length= %lld", content_length);

    int total_read_len = 0, read_len;

    char *buffer = malloc ((size_t) (content_length + (int64_t) 1));
    bzero (buffer, (size_t) (content_length + (int64_t) 1));

    if (total_read_len < content_length)
    {
        read_len = esp_http_client_read (client, buffer, content_length);

        if (read_len <= 0)
	    {
	        ESP_LOGE(TAG, "Error read data");
	        return ESP_FAIL;
	    }

        buffer[read_len] = 0;
        
        ESP_LOGI(TAG, "read_len = %d", read_len);
    }

    ESP_LOGI(TAG, "HTTP Stream reader Status = %d, content_length = %d",
	            esp_http_client_get_status_code (client),
	            (int) esp_http_client_get_content_length (client));

    free (buffer);

    err = esp_http_client_close(client);
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "Failed to close HTTP connection: %s", esp_err_to_name(err));
        esp_http_client_cleanup(client);
    }

    ESP_LOGW(TAG, "Limpiare");

    ESP_LOGI(TAG, "esp_get_free_heap_size: %ld", (long)esp_get_free_heap_size ());

    return ESP_OK;
}
And my function is working several times. After that I get an error like below:

Code: Select all

I (37509) Sending sendMessage: url string es: https://api.telegram.org/bot<TOKEN>/getUpdates?limit=1&offset=0
W (37509) Sending sendMessage: Iniciare
W (37519) Sending sendMessage: Enviare un mensaje a un chat
W (37519) Sending sendMessage: Open
E (38199) esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x2700
I (38199) esp-tls-mbedtls: Failed to verify peer certificate!
E (38199) esp-tls: Failed to open new connection
E (38209) transport_base: Failed to open a new connection
E (38219) HTTP_CLIENT: Connection failed, sock < 0
E (38219) HTTP_CLIENT Handler: Failed to open HTTP connection: ESP_ERR_HTTP_CONNECT
I (38229) HTTP_CLIENT Handler: HTTP_EVENT_DISCONNECTED
I (38229) HTTP_CLIENT Handler: Last esp error code: 0x801a
I (38239) HTTP_CLIENT Handler: Last mbedtls failure: 0x2700
I (38239) HTTP_CLIENT Handler: client connection open
I (38249) HTTP_CLIENT Handler: content_length= -1
I (38259) HTTP_CLIENT Handler: HTTP Stream reader Status = 0, content_length = 0
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x401787b5  PS      : 0x00060530  A0      : 0x800f3b16  A1      : 0x3fff4540
0x401787b5: esp_transport_get_error_handle at D:/esp-idf/components/tcp_transport/transport.c:255 (discriminator 1)

A2      : 0x00000077  A3      : 0x3f408994  A4      : 0x3f408c4c  A5      : 0x3fff4570
A6      : 0x3fff4550  A7      : 0x0000000c  A8      : 0x8017b7f4  A9      : 0x3fff44e0
A10     : 0x0000005c  A11     : 0x3ffc0b1c  A12     : 0x3fff4550  A13     : 0x0000000c
A14     : 0xffffffff  A15     : 0x00000003  SAR     : 0x00000004  EXCCAUSE: 0x0000001c
EXCVADDR: 0x000000af  LBEG    : 0x4008be39  LEND    : 0x4008be49  LCOUNT  : 0xfffffffa
0x4008be39: strlen at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/machine/xtensa/strlen.S:84

0x4008be49: strlen at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/machine/xtensa/strlen.S:96



Backtrace: 0x401787b2:0x3fff4540 0x400f3b13:0x3fff4560 0x400dc019:0x3fff4590 0x400dc07b:0x3fff5060 0x40091081:0x3fff5080
0x401787b2: esp_transport_get_error_handle at D:/esp-idf/components/tcp_transport/transport.c:254

0x400f3b13: esp_http_client_close at D:/esp-idf/components/esp_http_client/esp_http_client.c:1566

0x400dc019: getUpdates_test at D:/esp32s-deneme/cam-telegram/main/main.c:1077 (discriminator 13)

0x400dc07b: http_test_task at D:/esp32s-deneme/cam-telegram/main/main.c:800

0x40091081: vPortTaskWrapper at D:/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:162
I could not figure it out. Why this function fails?

Thanks in advance.

MicroController
Posts: 1704
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: HTTP server fails after a while.

Postby MicroController » Sat Nov 18, 2023 10:50 pm

Code: Select all

    esp_err_t err = esp_http_client_open(client, 0);
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
        esp_http_client_cleanup(client);
    }
...
   err = esp_http_client_close(client);
You clean up the client first, then try to close its connection.

Among other things, your code generally needs more structured error checking and handling.

User avatar
mbratch
Posts: 303
Joined: Fri Jun 11, 2021 1:51 pm

Re: HTTP server fails after a while.

Postby mbratch » Mon Nov 20, 2023 3:42 pm

mbedtls_ssl_handshake returned -0x2700
The error code means that the certificate failed verification. Are you sure your certificate is setup correctly?

hououin_kyouma
Posts: 7
Joined: Fri Nov 03, 2023 9:56 pm

Re: HTTP server fails after a while.

Postby hououin_kyouma » Thu Nov 30, 2023 8:31 pm

MicroController wrote:
Sat Nov 18, 2023 10:50 pm

Code: Select all

    esp_err_t err = esp_http_client_open(client, 0);
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
        esp_http_client_cleanup(client);
    }
...
   err = esp_http_client_close(client);
You clean up the client first, then try to close its connection.

Among other things, your code generally needs more structured error checking and handling.
Hi I've tried to clean up firstly. It's give an error instantly.

Code: Select all

I (2779) Sending sendMessage: url string es: https://api.telegram.org/bot<TOKEN>/getUpdates?limit=1&offset=0      
W (2789) Sending sendMessage: Iniciare
W (2799) Sending sendMessage: Enviare un mensaje a un chat
W (2799) Sending sendMessage: Open
I (2809) main_task: Returned from app_main()
I (2829) wifi:<ba-add>idx:0 (ifx:0, 7c:77:16:ac:d0:22), tid:0, ssn:5, winSize:64
I (3869) HTTP_CLIENT Handler: client connection open
I (3969) HTTP_CLIENT Handler: content_length= 23
I (3969) HTTP_CLIENT Handler: read_len = 23
I (3969) HTTP_CLIENT Handler: HTTP Stream reader Status = 200, content_length = 23
I (3969) HTTP_CLIENT Handler: HTTP_EVENT_DISCONNECTED
I (3979) HTTP_CLIENT Handler: HTTP_EVENT_DISCONNECTED
I (3979) HTTP_CLIENT Handler: Last esp error code: 0x40000de8
0x40000de8: _xtos_c_wrapper_handler in ROM

I (3989) HTTP_CLIENT Handler: Last mbedtls failure: 0x40000814
0x40000814: _xtos_l1int_handler in ROM

E (3999) HTTP_CLIENT Handler: Failed to close HTTP connection: ESP_FAIL
I (3999) HTTP_CLIENT Handler: HTTP_EVENT_DISCONNECTED

assert failed: tlsf_free tlsf.c:1119 (!block_is_free(block) && "block already marked as free")


Backtrace: 0x40081f45:0x3fff4370 0x4008e5d5:0x3fff4390 0x40096329:0x3fff43b0 0x4009377a:0x3fff44d0 0x40093614:0x3fff44f0 0x4008280e:0x3fff4510 0x40096359:0x3fff4530 0x4015eb49:0x3fff4550 0x400f3cbd:0x3fff4570 0x400dc06d:0x3fff4590 0x400dc0ab:0x3fff5060 0x40091081:0x3fff5080
0x40081f45: panic_abort at D:/esp-idf/components/esp_system/panic.c:452

0x4008e5d5: esp_system_abort at D:/esp-idf/components/esp_system/port/esp_system_chip.c:84

0x40096329: __assert_func at D:/esp-idf/components/newlib/assert.c:81

0x4009377a: tlsf_free at D:/esp-idf/components/heap/tlsf/tlsf.c:1119 (discriminator 1)

0x40093614: multi_heap_free_impl at D:/esp-idf/components/heap/multi_heap.c:231

0x4008280e: heap_caps_free at D:/esp-idf/components/heap/heap_caps.c:388

0x40096359: free at D:/esp-idf/components/newlib/heap.c:39

0x4015eb49: esp_transport_list_destroy at D:/esp-idf/components/tcp_transport/transport.c:75

0x400f3cbd: esp_http_client_cleanup at D:/esp-idf/components/esp_http_client/esp_http_client.c:858

0x400dc06d: getUpdates_test at D:/esp32s-deneme/cam-telegram/main/main.c:1089 (discriminator 3)

0x400dc0ab: http_test_task at D:/esp32s-deneme/cam-telegram/main/main.c:800

0x40091081: vPortTaskWrapper at D:/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:162





ELF file SHA256: 0668cf5805fd7871

Rebooting...

hououin_kyouma
Posts: 7
Joined: Fri Nov 03, 2023 9:56 pm

Re: HTTP server fails after a while.

Postby hououin_kyouma » Thu Nov 30, 2023 8:33 pm

mbratch wrote:
Mon Nov 20, 2023 3:42 pm
mbedtls_ssl_handshake returned -0x2700
The error code means that the certificate failed verification. Are you sure your certificate is setup correctly?
Hi I can use POST method in sendPhoto function continuosly without any problem with same certificate. When I try to GET method it crashes after a while.

Who is online

Users browsing this forum: No registered users and 115 guests