diff --git a/lib/WiFiManager/src/WiFiManagerHandlers.cpp b/lib/WiFiManager/src/WiFiManagerHandlers.cpp index 7820a33..c015ed5 100644 --- a/lib/WiFiManager/src/WiFiManagerHandlers.cpp +++ b/lib/WiFiManager/src/WiFiManagerHandlers.cpp @@ -1003,13 +1003,15 @@ void WiFiManagerHandlers::handleUpdateDone(AsyncWebServerRequest *request) { return; } + // AsyncWebServer queues this response and closes the connection only after + // it has completed the response. Restarting from this request callback—or + // merely waiting a guessed interval—can still tear down that TCP exchange. + // Schedule through process() after this particular response disconnects. + request->onDisconnect([this]() { + _wm->_rebootScheduled = true; + _wm->_rebootTime = millis() + _wm->REBOOT_DELAY_MS; + }); sendApiJson(request, 200, jsonApiOtaUpdateSuccess()); - // AsyncWebServer queues this response; restarting from the request callback - // tears down its TCP connection before the client can receive the success - // JSON. Let the application's regular process() call perform the same - // delayed restart path used by the other restart-capable portal endpoints. - _wm->_rebootScheduled = true; - _wm->_rebootTime = millis() + _wm->REBOOT_DELAY_MS; } void WiFiManagerHandlers::sendApiJson(AsyncWebServerRequest *request, int code, const String& json) {