How to fix ESP32 lwIP: assert failed: tcpip_inpkt (Invalid mbox)

Problem

Your microcontroller crashes with an assertion failure like

assert failed: tcpip_inpkt /IDF/components/lwip/lwip/src/api/tcpip.c:252 (Invalid mbox)

Solution

This error occurs because the TCP/IP stack has not been initialized before using it.

Note that esp_netif_init() calls tcpip_init() internally, so you don’t need to call tcpip_init() if you’re using esp_netif_init():

#include <lwip/tcpip.h>
#include <esp_netif.h>

void setup() {
  // Call this early in your setup function!
  esp_netif_init();
}

In case you don’t use the ESP high-level stack, make sure to call tcpip_init(nullptr, nullptr) before using any lwIP functions.

#include <lwip/tcpip.h>

void setup() {
  // Call this early in your setup function!
  tcpip_init(nullptr, nullptr);
}