Просмотр исходного кода

fix(http): two ways the web server could hang forever

The device has a single HTTP server task and the task watchdog is off,
so a handler that never returns takes the whole web UI and REST API
down until a power cycle.

- send_datafile / send_logfile ("last part" view of the log and data
  pages) skipped to the next line with while (fgetc(fd) != '\n'), which
  spins forever at EOF when the file has no newline, e.g. a 0-byte file
  left by a power cut. Stop at EOF too.
- upload_post_handler (and the softAP variant) retried
  HTTPD_SOCK_ERR_TIMEOUT forever. A client that disappears mid-upload
  without FIN/RST (Wi-Fi drop, phone goes to sleep) kept the task in the
  loop for good, with the file open. Give up after 6 consecutive
  timeouts (30 s), close and delete the partial file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Victor Gladkikh 1 месяц назад
Родитель
Сommit
75722a0df4
3 измененных файлов с 23 добавлено и 18 удалено
  1. 15 16
      code/components/jomjol_fileserver_ota/server_file.cpp
  2. 3 1
      code/include/defines.h
  3. 5 1
      code/main/softAP.cpp

+ 15 - 16
code/components/jomjol_fileserver_ota/server_file.cpp

@@ -393,14 +393,12 @@ static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)
             }
         }
 
-        /* Find end of line */
-        while (1)
+        /* Find end of line (a file without a newline, e.g. empty after a power cut, must not loop forever) */
+        int c;
+        do
         {
-            if (fgetc(fd) == '\n')
-            {
-                break;
-            }
-        }
+            c = fgetc(fd);
+        } while ((c != EOF) && (c != '\n'));
     }
 
     /* Retrieve the pointer to scratch buffer for temporary storage */
@@ -486,14 +484,12 @@ static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)
             }
         }
 
-        /* Find end of line */
-        while (1)
+        /* Find end of line (a file without a newline, e.g. empty after a power cut, must not loop forever) */
+        int c;
+        do
         {
-            if (fgetc(fd) == '\n')
-            {
-                break;
-            }
-        }
+            c = fgetc(fd);
+        } while ((c != EOF) && (c != '\n'));
     }
 
     /* Retrieve the pointer to scratch buffer for temporary storage */
@@ -704,6 +700,7 @@ static esp_err_t upload_post_handler(httpd_req_t *req)
     /* Content length of the request gives
      * the size of the file being uploaded */
     int remaining = req->content_len;
+    int timeouts = 0;
 
     while (remaining > 0)
     {
@@ -712,9 +709,10 @@ static esp_err_t upload_post_handler(httpd_req_t *req)
         /* Receive the file part by part into a buffer */
         if ((received = httpd_req_recv(req, buf, MIN(remaining, SERVER_FILER_SCRATCH_BUFSIZE))) <= 0)
         {
-            if (received == HTTPD_SOCK_ERR_TIMEOUT)
+            /* Retry if timeout occurred, but give up when the client is gone (no FIN/RST after a
+             * Wi-Fi drop): retrying forever blocks the only HTTP server task */
+            if ((received == HTTPD_SOCK_ERR_TIMEOUT) && (++timeouts < UPLOAD_MAX_RECV_TIMEOUTS))
             {
-                /* Retry if timeout occurred */
                 continue;
             }
 
@@ -746,6 +744,7 @@ static esp_err_t upload_post_handler(httpd_req_t *req)
         /* Keep track of remaining size of
          * the file left to be uploaded */
         remaining -= received;
+        timeouts = 0;
     }
 
     /* Close file upon upload completion */

+ 3 - 1
code/include/defines.h

@@ -93,7 +93,9 @@
          
     #define LOGFILE_LAST_PART_BYTES 80 * 1024 // 80 kBytes  // Size of partial log file to return 
 
-    #define SERVER_FILER_SCRATCH_BUFSIZE  4096 
+    #define UPLOAD_MAX_RECV_TIMEOUTS 6 // Uploads give up after this many consecutive receive timeouts (recv_wait_timeout = 5 s each)
+
+    #define SERVER_FILER_SCRATCH_BUFSIZE  4096
     #define SERVER_HELPER_SCRATCH_BUFSIZE  4096
     #define SERVER_OTA_SCRATCH_BUFSIZE  1024 
 

+ 5 - 1
code/main/softAP.cpp

@@ -409,11 +409,14 @@ esp_err_t upload_post_handlerAP(httpd_req_t *req)
 
 
 
+    int timeouts = 0;
+
     while (remaining > 0) {
 
         ESP_LOGI(TAG, "Remaining size: %d", remaining);
         if ((received = httpd_req_recv(req, buf, MIN(remaining, 1024))) <= 0) {
-            if (received == HTTPD_SOCK_ERR_TIMEOUT) {
+            // Give up when the client is gone, retrying forever blocks the HTTP server task
+            if ((received == HTTPD_SOCK_ERR_TIMEOUT) && (++timeouts < UPLOAD_MAX_RECV_TIMEOUTS)) {
                 continue;
             }
 
@@ -435,6 +438,7 @@ esp_err_t upload_post_handlerAP(httpd_req_t *req)
         }
 
         remaining -= received;
+        timeouts = 0;
     }
     fclose(fd);
     isConfigINI = true;