From 5722889fe31b44e22f5f77e71044bbfb363eb909 Mon Sep 17 00:00:00 2001 From: "hrushikesh.bhosale" Date: Fri, 25 Apr 2025 11:00:10 +0530 Subject: [PATCH] fix(esp_http_server): Async handler example stack overflow fix 1. In async handler example, on hitting /long URI and closing the connection forcefully from client (example ctrl + c) cause more stack size (almost 200 bytes) than successfull request. 2. The connection should be closed from the server as soon as the client closes the connect (i.e. handler should return ESP_FAIL to close the connection) --- .../async_handlers/main/Kconfig.projbuild | 6 ++++++ .../http_server/async_handlers/main/main.c | 20 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/examples/protocols/http_server/async_handlers/main/Kconfig.projbuild b/examples/protocols/http_server/async_handlers/main/Kconfig.projbuild index 263ec76bff..0e717b6aca 100644 --- a/examples/protocols/http_server/async_handlers/main/Kconfig.projbuild +++ b/examples/protocols/http_server/async_handlers/main/Kconfig.projbuild @@ -7,4 +7,10 @@ menu "Example Configuration" The maximum number of simultaneous async requests that the web server can handle. + config EXAMPLE_ASYNC_WORKER_TASK_STACK_SIZE + int "Async Worker Task Stack Size" + default 2560 + help + The stack size allocated for each async worker task. + endmenu diff --git a/examples/protocols/http_server/async_handlers/main/main.c b/examples/protocols/http_server/async_handlers/main/main.c index 7a1091c10d..17417204ee 100644 --- a/examples/protocols/http_server/async_handlers/main/main.c +++ b/examples/protocols/http_server/async_handlers/main/main.c @@ -32,7 +32,7 @@ */ #define ASYNC_WORKER_TASK_PRIORITY 5 -#define ASYNC_WORKER_TASK_STACK_SIZE 2048 +#define ASYNC_WORKER_TASK_STACK_SIZE CONFIG_EXAMPLE_ASYNC_WORKER_TASK_STACK_SIZE static const char *TAG = "example"; @@ -133,7 +133,11 @@ static esp_err_t long_async_handler(httpd_req_t *req) // send a request count char s[100]; snprintf(s, sizeof(s), "
req: %u
\n", req_count); - httpd_resp_sendstr_chunk(req, s); + esp_err_t err = httpd_resp_sendstr_chunk(req, s); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send string chunk: %s", esp_err_to_name(err)); + return err; + } // then every second, send a "tick" for (int i = 0; i < 60; i++) { @@ -145,11 +149,19 @@ static esp_err_t long_async_handler(httpd_req_t *req) // send a tick snprintf(s, sizeof(s), "
%u
\n", i); - httpd_resp_sendstr_chunk(req, s); + err = httpd_resp_sendstr_chunk(req, s); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send string chunk: %s", esp_err_to_name(err)); + return err; + } } // send "complete" - httpd_resp_sendstr_chunk(req, NULL); + err = httpd_resp_sendstr_chunk(req, NULL); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send string chunk: %s", esp_err_to_name(err)); + return err; + } return ESP_OK; }