Skip to content

Commit c8a634f

Browse files
committed
Fix: request->abort() was not firing onDisconnect()
1 parent 85a9ea0 commit c8a634f

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/WebHandlers.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,11 @@ bool AsyncStaticWebHandler::_searchFile(AsyncWebServerRequest *request, const St
178178
char *_tempPath = (char *)malloc(pathLen + 1);
179179
if (_tempPath == NULL) {
180180
async_ws_log_e("Failed to allocate");
181-
request->abort();
181+
// abort() now triggers _onDisconnect() → delete request (it no longer
182+
// just sends an RST), so we must close the file BEFORE aborting to avoid
183+
// a use-after-free on the destroyed request object.
182184
request->_tempFile.close();
185+
request->abort();
183186
return false;
184187
}
185188
snprintf_P(_tempPath, pathLen + 1, PSTR("%s"), path.c_str());

src/WebRequest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,18 @@ void AsyncWebServerRequest::abort() {
10671067
_paused = false;
10681068
_this.reset();
10691069
// async_ws_log_e("AsyncWebServerRequest::abort");
1070+
// abort() sends a TCP RST (immediate reset, no TIME_WAIT) and frees the lwIP
1071+
// pcb — the desired behavior for protocol violations. However, abort() does
1072+
// NOT fire the onDisconnect (_discard_cb) callback on any platform (ESP32
1073+
// AsyncTCP, ESP8266 ESPAsyncTCP, RPAsyncTCP), so the AsyncWebServerRequest +
1074+
// AsyncClient + _response + _tempObject + _tempFile + _itemBuffer would
1075+
// leak permanently. Manually trigger _onDisconnect() to run the user
1076+
// callback and _server->_handleDisconnect(this) → delete this → ~AsyncWebServerRequest
1077+
// → delete _client (pcb is already null from abort, so ~AsyncClient is a no-op).
1078+
// Safe because all call sites return immediately after abort() — no member
1079+
// is accessed after _onDisconnect() destroys *this.
10701080
_client->abort();
1081+
_onDisconnect();
10711082
}
10721083
}
10731084

0 commit comments

Comments
 (0)