From 6906c1b65b723855853429054dd7ca6b37b6c899 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Tue, 21 Oct 2025 09:08:26 -0700 Subject: [PATCH] Fix LWIP failure after 256 Ethernet disconnects Thanks to Yohine. He identified via email a leak of DHCP state that would cause LWIP to panic() after 256 disconnects. Properly clean up DHCP state on link ::end (shutdown). --- libraries/lwIP_Ethernet/src/LwipIntfDev.h | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/libraries/lwIP_Ethernet/src/LwipIntfDev.h b/libraries/lwIP_Ethernet/src/LwipIntfDev.h index ac8803de6..b2afe8898 100644 --- a/libraries/lwIP_Ethernet/src/LwipIntfDev.h +++ b/libraries/lwIP_Ethernet/src/LwipIntfDev.h @@ -350,6 +350,8 @@ bool LwipIntfDev::begin(const uint8_t* macAddress, const uint16_t mtu) { lwip_init(); __startEthernetContext(); + memset(&_netif, 0, sizeof(_netif)); + if (RawDev::needsSPI()) { _spiUnit.begin(); // Set SPI clocks/etc. per request, doesn't seem to be direct way other than a fake transaction @@ -390,8 +392,7 @@ bool LwipIntfDev::begin(const uint8_t* macAddress, const uint16_t mtu) { _netif.hostname = wifi_station_hostname; - if (!netif_add(&_netif, ip_2_ip4(&ip_addr), ip_2_ip4(&netmask), ip_2_ip4(&gw), this, - netif_init_s, ethernet_input)) { + if (!netif_add(&_netif, ip_2_ip4(&ip_addr), ip_2_ip4(&netmask), ip_2_ip4(&gw), this, netif_init_s, ethernet_input)) { return false; } @@ -410,15 +411,10 @@ bool LwipIntfDev::begin(const uint8_t* macAddress, const uint16_t mtu) { // Start a new DHCP request _netif.flags |= NETIF_FLAG_UP; - switch (dhcp_start(&_netif)) { - case ERR_OK: - break; - - case ERR_IF: - netif_remove(&_netif); - return false; - - default: + if (dhcp_start(&_netif) != ERR_OK) { + if (_intrPin < 0) { + __removeEthernetPacketHandler(_phID); + } netif_remove(&_netif); return false; } @@ -465,6 +461,9 @@ extern std::function _removeNetifCB; template void LwipIntfDev::end() { if (_started) { + if (_isDHCP) { + dhcp_stop(&_netif); + } if (_intrPin < 0) { __removeEthernetPacketHandler(_phID); } else {