From 7e676c1055172a731d576ad36c025a22b5eeb33f Mon Sep 17 00:00:00 2001 From: Anurag Kar Date: Mon, 1 Apr 2019 14:52:04 +0530 Subject: [PATCH] esp_http_server : Only accept new connections if server has capacity to handle more This fix prevents HTTP server from accepting new connections when the total count of connected sockets has reached the max_open_sockets limit set during configuration. The pending connections are kept in backlog until atleast one of the connected sockets is closed. The maximum number of connection requests that can kept in backlog is specified as backlog_conn configuration option. Note that this modification has no effect when LRU purge is enabled. Also added sanity check on setting for max_open_sockets during configuration. Solution suggested by jimparis https://github.com/espressif/esp-idf/issues/3183#issue-421234265 Closes https://github.com/espressif/esp-idf/issues/3183 --- components/esp_http_server/src/httpd_main.c | 24 ++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/components/esp_http_server/src/httpd_main.c b/components/esp_http_server/src/httpd_main.c index 41a0e75e8..d49970838 100644 --- a/components/esp_http_server/src/httpd_main.c +++ b/components/esp_http_server/src/httpd_main.c @@ -156,7 +156,12 @@ static esp_err_t httpd_server(struct httpd_data *hd) { fd_set read_set; FD_ZERO(&read_set); - FD_SET(hd->listen_fd, &read_set); + if (hd->config.lru_purge_enable || httpd_is_sess_available(hd)) { + /* Only listen for new connections if server has capacity to + * handle more (or when LRU purge is enabled, in which case + * older connections will be closed) */ + FD_SET(hd->listen_fd, &read_set); + } FD_SET(hd->ctrl_fd, &read_set); int tmp_max_fd; @@ -348,6 +353,23 @@ esp_err_t httpd_start(httpd_handle_t *handle, const httpd_config_t *config) return ESP_ERR_INVALID_ARG; } + /* Sanity check about whether LWIP is configured for providing the + * maximum number of open sockets sufficient for the server. Though, + * this check doesn't guarantee that many sockets will actually be + * available at runtime as other processes may use up some sockets. + * Note that server also uses 3 sockets for its internal use : + * 1) listening for new TCP connections + * 2) for sending control messages over UDP + * 3) for receiving control messages over UDP + * So the total number of required sockets is max_open_sockets + 3 + */ + if (CONFIG_LWIP_MAX_SOCKETS < config->max_open_sockets + 3) { + ESP_LOGE(TAG, "Configuration option max_open_sockets is too large (max allowed %d)\n\t" + "Either decrease this or configure LWIP_MAX_SOCKETS to a larger value", + CONFIG_LWIP_MAX_SOCKETS - 3); + return ESP_ERR_INVALID_ARG; + } + struct httpd_data *hd = httpd_create(config); if (hd == NULL) { /* Failed to allocate memory */