Search code examples
c++arduinowebserveresp32

How to end AsyncWebServer ESP32


Let's say that I'd like to end my server my code looks like this:

#include <WiFi.h>
#include <ESPAsyncWebServer.h>
AsyncWebServer server(7777);
const char* ssid = "SSID";
const char* password = "PASSWORD";

void notFound(AsyncWebServerRequest *request) {
  Serial.println(request->url());
  request->send(404, "text/plain", "Not found");
}

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi Failed!");
    return;
  }
  server.onNotFound(notFound);
  server.begin();
  server.end();
}

void loop() {

}

Please note that I know that this code will close the server as soon as it starts but It's just the shortest code to reproduce my problem. The ESP32 automatically resets after it reaches the line server.end(); The error message

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8
assertion "invalid socket state for sent callback" failed: file "/home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp.c", line 1842, function: tcp_sent
abort() was called at PC 0x40101537 on core 1

Backtrace: 0x4008c434:0x3ffb1e90 0x4008c665:0x3ffb1eb0 0x40101537:0x3ffb1ed0 0x40119582:0x3ffb1f00 0x400d5e7d:0x3ffb1f20 0x400d5149:0x3ffb1f40 0x400d10d2:0x3ffb1f60 0x400d7e87:0x3ffb1fb0 0x40088b7d:0x3ffb1fd0

Rebooting...
ets Jun  8 2016 00:22:57

Solution

  • server.end(); works fine, I just messed up something with the library