Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Connection getting closed unexpectedly

Hi, I am having issues with my ESP32 running mongoose networking library. My device is syncing to a server and every once in a while the device will try to sync and instantly say the connection was closed before sending any data.

I set up a wireshark to catch the packets to and from the device and have found that right before the connection closes there is a TCP [FIN, ACK] re-transmission from the server and a subsequent DUP-ACK in response. this is often a re-transmission from an old connection up to about 30 seconds prior.

This [FIN, ACK] re-transmission, even though it is from an older connection, seems to confuse the new connection into closing instantly.

Attached is an example of what I have seen in wireshark. The info blocked out in red is the server I am trying to connect to.

Here is a readout from the mongoose debug mode:

mg_http_common_url_p SERVER_URL:80 /index.php/v1/device
mg_connect_http_base SERVER_URL/index.php/v1/device use_ssl? 0
mg_resolve_async_opt SERVER_URL 1 0x3ffddfa4
mg_do_connect 0x3ffe3b24 udp://8.8.8.8:53
mg_add_conn 0x3ffb4630 0x3ffe3b24
mg_mgr_handle_conn 0x3ffe3b24 fd=1 fd_flags=2 nc_flags=10 rmbl=0 smbl=0
mg_if_connect_cb 0x3ffe3b24 connect, err=0
mg_call 0x3ffe3b24 user ev=2 ev_data=0x3ffddfe0 flags=2 rmbl=0 smbl=0
mg_resolve_async_eh ev=2 user_data=0x3fff24b8
mg_send_dns_query SERVER_URL 1
mg_call 0x3ffe3b24 after user flags=2 rmbl=0 smbl=41
mg_write_to_socket 0x3ffe3b24 1 41 119 8.8.8.8:53
mg_call 0x3ffe3b24 user ev=4 ev_data=0x3ffddfb0 flags=2 rmbl=0 smbl=0
mg_resolve_async_eh ev=4 user_data=0x3fff24b8
mg_call 0x3ffe3b24 after user flags=2 rmbl=0 smbl=0
mg_mgr_handle_conn 0x3ffe3b24 after fd=1 nc_flags=2 rmbl=0 smbl=0
mg_mgr_handle_conn 0x3ffe1b28 fd=6 fd_flags=1 nc_flags=0 rmbl=0 smbl=0
mg_handle_tcp_read 0x3ffe1b28 0 bytes (PLAIN) <- 6
mg_mgr_handle_conn 0x3ffe1b28 after fd=6 nc_flags=1024 rmbl=0 smbl=0
mg_close_conn 0x3ffe1b28 1024 6
mg_call 0x3ffe1b28 proto ev=5 ev_data=0x0 flags=1024 rmbl=0 smbl=0
mg_call 0x3ffe1b28 user ev=5 ev_data=0x0 flags=1024 rmbl=0 smbl=0
E (293434) Wifi: Server closed connection
mg_call 0x3ffe1b28 after user flags=1024 rmbl=0 smbl=0
mg_call 0x3ffe1b28 after proto flags=1024 rmbl=0 smbl=0

Here is the relevant code:

static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
    struct http_message *hm = (struct http_message *) ev_data;

    switch (ev) {
    case MG_EV_CONNECT:
        if (*(int *)ev_data != 0) {
            ESP_LOGE(TAG, "connect() failed: %d\n", *(int *)ev_data);
            s_exit_flag = 1;
            error = ESP_ERR_WIFI_CONN;
        }
        break;
    case MG_EV_HTTP_REPLY:
        nc->flags |= MG_F_SEND_AND_CLOSE;       //  i have also tried close immediately

        //malloc and copy response
        *response_temp = (char*)malloc(hm->body.len * sizeof(char));
        memcpy(*response_temp, hm->body.p, hm->body.len * sizeof(char));
        response_size = hm->body.len * sizeof(char);
        s_exit_flag = 1;
        break;
    case MG_EV_CLOSE:
        if (s_exit_flag == 0) {
            ESP_LOGE(TAG, "Server closed connection\n");
            s_exit_flag = 1;
            error = ESP_ERR_TIMEOUT;
        }
        break;
    case MG_EV_TIMER:
        nc->flags |= MG_F_SEND_AND_CLOSE;      // i have also tried close immediately
        s_exit_flag = 1;
        error = ESP_ERR_TIMEOUT;
        break;
    default:
        break;
    }
}

esp_err_t wifi_send_data(const char * data, const char * headers, char ** response) {
    error = ESP_OK;
    struct mg_connection *nc;
    s_exit_flag = 0;//reset each run
    response_temp = response;

    nc = mg_connect_http(&mgr, ev_handler, WEB_URL, headers, data);
    mg_set_protocol_http_websocket(nc);

    while (s_exit_flag == 0) {
        mg_mgr_poll(&mgr, 100);
    }

    //mg_mgr_free(&mgr);

    return error;
}

Note: mgr is not freed here as it is re-used to send more data.

Any help is greatly appreciated.

  • BrettG

Comments

Sign In or Register to comment.