Skip to content

[esp_http_client]: async client triggers spurious HTTP_EVENT_ERROR before connecting (IDFGH-15428) #16075

Open
@bryghtlabs-richard

Description

@bryghtlabs-richard

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

IDF version.

v5.1.2, v5.1.6, v5.4

Espressif SoC revision.

ESP32-S3

Operating System used.

Windows

How did you build your project?

Command line with idf.py

If you are using Windows, please specify command line type.

CMD

Development Kit.

custom, NodeMCU ESP-32S

Power Supply used.

USB

What is the expected behavior?

I would expect HTTP_EVENT_ERROR to occur when something bad happens.

What is the actual behavior?

A spurious HTTP_EVENT_ERROR often appears when esp_http_client_open() opens a connection in async mode.

Steps to reproduce.

  1. Turn on debug logging(CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y) for examples/protocols/esp_http_client - need to see ESP_LOGD() output in main.c
  2. patch app_main() to call below function, run it
  3. Observe https_async_spurious_error() triggers a spurious HTTP_EVENT_ERROR before the client is even connected.

Here is the client-function:

static void https_async_spurious_error(void)
{
    esp_http_client_config_t config = {
        .url = "https://postman-echo.com/post",
        .event_handler = _http_event_handler,
        .cert_pem = postman_root_cert_pem_start,
        .is_async = true,
        .timeout_ms = 5000,
    };
    esp_http_client_handle_t client = esp_http_client_init(&config);
    esp_err_t err;
    const char *post_data = "Using a Palantír requires a person with great strength of will and wisdom. The Palantíri were meant to "
                            "be used by the Dúnedain to communicate throughout the Realms in Exile. During the War of the Ring, "
                            "the Palantíri were used by many individuals. Sauron used the Ithil-stone to take advantage of the users "
                            "of the other two stones, the Orthanc-stone and Anor-stone, but was also susceptible to deception himself.";
    esp_http_client_set_method(client, HTTP_METHOD_POST);
    esp_http_client_set_post_field(client, post_data, strlen(post_data));

    err = esp_http_client_open(client, strlen(post_data));

    ESP_LOGI(TAG, "esp_http_client_open()=%i", err);

    esp_http_client_cleanup(client);
}

Debug Logs.

I (6063) example_common: Connected to example_netif_sta
D (6073) esp_netif_lwip: esp_netif_get_ip_info esp_netif:0x3ffc67f8
I (6073) example_common: - IPv4 address: 192.168.1.37,
I (6083) example_common: - IPv6 address: fe80:0000:0000:0000:266f:28ff:feb0:858c, type: ESP_IP6_ADDR_IS_LINK_LOCAL
D (6093) esp_netif_lwip: call api in lwip: ret=0x0, give sem
I (6093) HTTP_CLIENT: Connected to AP, begin http example
D (6103) HTTP_CLIENT: set post file length = 415
D (6103) HTTP_CLIENT: Begin connect to: https://postman-echo.com:443
D (6113) esp-tls: host:postman-echo.com: strlen 16
I (6123) main_task: Returned from app_main()
D (6123) wifi:eb is dhcp or dns sport = 18800, dport = 53
D (6363) esp-tls: [sock=54] Resolved IPv4 address: 44.212.217.153
D (6363) esp-tls: [sock=54] Connecting to server. HOST: postman-echo.com, Port: 443
D (6373) esp-tls: connecting...
D (6423) esp-tls: handshake in progress...
D (6423) HTTP_CLIENT: Connection not yet established
D (6423) HTTP_CLIENT: HTTP_EVENT_ERROR ###THIS IS SPURIOUS, CLIENT STILL OPENING
I (6423) HTTP_CLIENT: esp_http_client_open()=28678
D (6423) event: no handlers have been registered for event ESP_HTTP_CLIENT_EVENT:0 posted to loop 0x3ffbd284
I (6433) HTTP_CLIENT: HTTP_EVENT_DISCONNECTED
D (6443) event: no handlers have been registered for event ESP_HTTP_CLIENT_EVENT:6 posted to loop 0x3ffbd284
I (6443) HTTP_CLIENT: Finish http example
D (8023) esp_netif_lwip: esp_netif_internal_nd6_cb lwip-netif:0x3ffc6874
D (8023) esp_netif_lwip: check: local, if=0x3ffc67f8 fn=0x400e0064
D (8023) esp_netif_lwip: esp_netif_update_default_netif_lwip 0x3ffc67f8
D (8033) event: running post IP_EVENT:3 with handler 0x400d9544 and context 0x3ffc7434 on loop 0x3ffbd284
I (8033) example_connect: Got IPv6 event: Interface "example_netif_sta" address: 2605:a601:adbb:2600:266f:28ff:feb0:858c, type: ESP_IP6_ADDR_IS_GLOBAL

Diagnostic report archive.

No response

More Information.

I think the bug(s) are here, marked with TODO:

esp_err_t esp_http_client_open(esp_http_client_handle_t client, int write_len)
{
    client->post_len = write_len;
    esp_err_t err;
    if ((err = esp_http_client_connect(client)) != ESP_OK) {
        //TODO: Bail out if error is recoverable
        http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
        http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t));
        return err;
    }
    if ((err = esp_http_client_request_send(client, write_len)) != ESP_OK) {
        //TODO: Bail out if error is recoverable
        http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
        http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t));
        return err;
    }
    return ESP_OK;
}

esp_http_client_example.c.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions