Skip to content

Commit e6c7b97

Browse files
Adjust LWIP intf to avoid hangs/crashes under load (#1286)
It seems possible now for TCP connection _pcbs to disappear while being processed, due to the new async context configuration. This would cause LWIP to panic when a NULL pcb was passed in. Check for and avoid passing in null PCBs in the ClientContext. Undo special-casing of sys_check_timeouts wrapper AdvancedWebServer with heavy F5-refresh and #1274 test both pass. Fixes #1274
1 parent 54f9d3c commit e6c7b97

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

cores/rp2040/lwip_wrap.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,10 @@ extern "C" {
256256
}
257257

258258
// sys_check_timeouts is special case because the async process will call it. If we're already in a timeout check, just do a noop
259-
auto_init_mutex(__sys_check_timeouts_mtx);
260-
extern void __real_sys_check_timeouts(void);
259+
extern void __real_sys_check_timeouts();
261260
void __wrap_sys_check_timeouts(void) {
262-
uint32_t owner;
263-
if (mutex_try_enter(&__sys_check_timeouts_mtx, &owner)) {
264-
__real_sys_check_timeouts();
265-
mutex_exit(&__sys_check_timeouts_mtx);
266-
}
261+
LWIPMutex m;
262+
__real_sys_check_timeouts();
267263
}
268264

269265
extern err_t __real_dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found, void *callback_arg);

libraries/WiFi/src/include/ClientContext.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ class ClientContext {
328328
return false;
329329
}
330330

331-
331+
if (!_pcb) {
332+
return false;
333+
}
332334
// force lwIP to send what can be sent
333335
tcp_output(_pcb);
334336

@@ -522,6 +524,9 @@ class ClientContext {
522524
const auto remaining = _datalen - _written;
523525
size_t next_chunk_size;
524526
{
527+
if (!_pcb) {
528+
return false;
529+
}
525530
next_chunk_size = std::min((size_t)tcp_sndbuf(_pcb), remaining);
526531
// Potentially reduce transmit size if we are tight on memory, but only if it doesn't return a 0 chunk size
527532
if (next_chunk_size > (size_t)(1 << scale)) {
@@ -550,6 +555,9 @@ class ClientContext {
550555
flags |= TCP_WRITE_FLAG_COPY;
551556
}
552557

558+
if (!_pcb) {
559+
return false;
560+
}
553561
err_t err = tcp_write(_pcb, buf, next_chunk_size, flags);
554562

555563
DEBUGV(":wrc %d %d %d\r\n", next_chunk_size, remaining, (int)err);
@@ -571,7 +579,7 @@ class ClientContext {
571579
}
572580
}
573581

574-
if (has_written) {
582+
if (has_written && _pcb) {
575583
// lwIP's tcp_output doc: "Find out what we can send and send it"
576584
// *with respect to Nagle*
577585
// more info: https://lists.gnu.org/archive/html/lwip-users/2017-11/msg00134.html

libraries/lwIP_CYW43/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version=1
33
author=Earle F. Philhower, III
44
maintainer=Earle F. Philhower, III <[email protected]>
55
sentence=RP2040 Cyw43XX wifi driver
6-
paragraph=Driver for the raspberry Pi Pico W wireless chip, CYW43439, to integrate witrh arduino-pico
6+
paragraph=Driver for the Raspberry Pi Pico W wireless chip, CYW43439, to integrate with arduino-pico
77
category=Communication
88
url=https://github.com/earlephilhower/arduino-pico
99
architectures=rp2040

0 commit comments

Comments
 (0)