diff --git a/build/src/mongoose.c b/build/src/mongoose.c
index 8c49ffbbd0dd022b26a8594f89cdfef3ec953140..bee96cc4abe30f022dc2d413e748b2ea152bb9cb 100644
--- a/build/src/mongoose.c
+++ b/build/src/mongoose.c
@@ -145,6 +145,10 @@ static void send_http_error(struct mg_connection *conn, int status,
               suggest_connection_header(conn));
     conn->num_bytes_sent += mg_printf(conn, "%s", buf);
   }
+
+  if (status >= 500) {
+    conn->must_close = 1;
+  }
 }
 
 // Return 1 if real file has been found, 0 otherwise
diff --git a/mongoose.c b/mongoose.c
index 6a4a527932e933bcacf86e5236463a46915ba5ba..753092372a84cba37c02b6f30924e8449e16538f 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -4016,6 +4016,10 @@ static void send_http_error(struct mg_connection *conn, int status,
               suggest_connection_header(conn));
     conn->num_bytes_sent += mg_printf(conn, "%s", buf);
   }
+
+  if (status >= 500) {
+    conn->must_close = 1;
+  }
 }
 
 // Return 1 if real file has been found, 0 otherwise