Fix WiFi file transfer lockup and add performance optimizations
This commit resolves critical stability issues causing WiFi file transfer
crashes in STA mode and adds aggressive performance optimizations for
maximum upload/download throughput.
CRITICAL BUG FIXES:
1. Fix use-after-free race condition in handleClient()
- Added atomic operations and mutex protection for server pointer
- Prevents store access fault crashes when stop() is called during handleClient()
- Root cause of the Guru Meditation Error in crash logs
- Location: src/network/CrossPointWebServer.cpp:135-158
2. Fix JSON buffer overflow in handleFileListData()
- Replaced 512-byte static buffer with dynamic allocation
- Safely handles 500-character filenames with JSON escaping
- Prevents stack corruption from oversized file entries
- Location: src/network/CrossPointWebServer.cpp:253-306
3. Convert static upload variables to thread-safe instance variables
- Moved uploadFile, uploadFileName, uploadPath, etc. to class members
- Added mutex protection to prevent concurrent access corruption
- Eliminates race conditions during uploads
- Location: src/network/CrossPointWebServer.h:44-54, CrossPointWebServer.cpp:311-313
4. Add yield() to handleClient loop
- Prevents WiFi stack starvation in STA mode
- Allows LWIP to process incoming packets and prevent buffer overflow
- Critical for stability during file transfers
- Location: src/activities/network/CrossPointWebServerActivity.cpp:304-311
5. Fix heap exhaustion with String pre-allocation
- Pre-allocate String capacities to avoid reallocations during upload
- Add heap threshold check (50KB minimum) before accepting uploads
- Reduces memory fragmentation
- Location: src/network/CrossPointWebServer.cpp:323-376
ROBUSTNESS IMPROVEMENTS:
6. Increase task stack size from 2KB to 6KB
- Prevents stack overflow during rendering + network operations
- Aligns with other display+network activity stack sizes
- Location: src/activities/network/CrossPointWebServerActivity.cpp:51
PERFORMANCE OPTIMIZATIONS:
7. Add LWIP TCP/IP stack optimizations
- Increased TCP MSS to 1436 bytes (optimized for WiFi)
- Increased send/receive buffers to 5744 bytes (4x MSS)
- Enlarged TCP/IP mailbox sizes for better packet handling
- Increased retransmission timeout to 3000ms (accommodates SD writes)
- Location: platformio.ini:31-49
8. Add WiFi performance optimizations
- Increased WiFi RX/TX buffer counts for better throughput
- Configured dynamic buffer allocation for optimal memory usage
- Enabled TCP window scaling and oversizing
- Location: platformio.ini:41-49
9. Maximize WiFi TX power
- Set WiFi TX power to maximum (19.5dBm) for best signal strength
- Improves throughput, especially at distance
- Applied to both STA and AP modes
- Location: src/network/CrossPointWebServer.cpp:58,
src/activities/network/CrossPointWebServerActivity.cpp:152,192
EXPECTED IMPACT:
- Eliminates WiFi file transfer crashes in STA mode
- Improves upload/download speeds by 2-3x through TCP optimizations
- Increases stability during large file transfers (>100MB)
- Better performance on weak WiFi signals
- Reduces heap fragmentation and memory pressure
TESTING RECOMMENDATIONS:
1. Test large file uploads (>50MB) in both STA and AP modes
2. Verify system stability during concurrent uploads
3. Monitor heap usage during file transfers
4. Test with long filenames (400+ characters)
5. Verify performance improvement with speed tests
Fixes: WiFi lockup bug causing Guru Meditation Errors during file transfer
This commit is contained in:
@@ -48,7 +48,7 @@ void CrossPointWebServerActivity::onEnter() {
|
||||
updateRequired = true;
|
||||
|
||||
xTaskCreate(&CrossPointWebServerActivity::taskTrampoline, "WebServerActivityTask",
|
||||
2048, // Stack size
|
||||
6144, // Stack size (increased from 2KB to 6KB for stability)
|
||||
this, // Parameters
|
||||
1, // Priority
|
||||
&displayTaskHandle // Task handle
|
||||
@@ -147,6 +147,11 @@ void CrossPointWebServerActivity::onNetworkModeSelected(const NetworkMode mode)
|
||||
// AP mode - start access point
|
||||
state = WebServerActivityState::AP_STARTING;
|
||||
updateRequired = true;
|
||||
|
||||
// WiFi performance optimizations for AP mode
|
||||
WiFi.setSleep(false); // Disable WiFi sleep
|
||||
WiFi.setTxPower(WIFI_POWER_19_5dBm); // Maximum TX power for ESP32-C3
|
||||
|
||||
startAccessPoint();
|
||||
}
|
||||
}
|
||||
@@ -187,6 +192,12 @@ void CrossPointWebServerActivity::startAccessPoint() {
|
||||
WiFi.mode(WIFI_AP);
|
||||
delay(100);
|
||||
|
||||
// WiFi performance optimizations for maximum throughput
|
||||
WiFi.setSleep(false); // Disable WiFi sleep
|
||||
WiFi.setTxPower(WIFI_POWER_19_5dBm); // Maximum TX power for ESP32-C3
|
||||
|
||||
Serial.printf("[%lu] [WEBACT] WiFi optimizations applied (sleep disabled, max TX power)\n", millis());
|
||||
|
||||
// Start soft AP
|
||||
bool apStarted;
|
||||
if (AP_PASSWORD && strlen(AP_PASSWORD) >= 8) {
|
||||
@@ -300,6 +311,15 @@ void CrossPointWebServerActivity::loop() {
|
||||
constexpr int HANDLE_CLIENT_ITERATIONS = 10;
|
||||
for (int i = 0; i < HANDLE_CLIENT_ITERATIONS && webServer->isRunning(); i++) {
|
||||
webServer->handleClient();
|
||||
|
||||
// CRITICAL: Yield to WiFi stack and other tasks between iterations
|
||||
// This prevents WiFi stack starvation in STA mode and improves stability
|
||||
yield();
|
||||
|
||||
// Add small delay every few iterations to reduce CPU pressure
|
||||
if (i % 3 == 2) {
|
||||
delay(1); // 1ms delay every 3 iterations
|
||||
}
|
||||
}
|
||||
lastHandleClientTime = millis();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user