Skip to content

fix(webconfig): rebind port 80 right after a stop - #97

Merged
agessaman merged 1 commit into
observer-firmware-devfrom
fix/port80-rebind
Nov 12, 2026
Merged

agessaman merged 1 commit into
observer-firmware-devfrom
fix/port80-rebind

Conversation

@agessaman

Copy link
Copy Markdown
Owner

Problem

After stop webconfig, starting anything on port 80 again (start webconfig or start ota) could fail for up to 120 s with bind error: -8 (ERR_USE), and the server did not come up.

The listener is not what holds the port. The last accepted HTTP connection sits in TIME_WAIT for 2 x MSL (120 s here), and lwIP's tcp_bind only ignores a TIME_WAIT pcb when the new pcb has SOF_REUSEADDR. AsyncTCP never sets it.

Fix

Set SOF_REUSEADDR on the pcb before a bind to local port 80.

  • -Wl,--wrap=tcp_bind in [esp32_base], next to the existing dns_gethostbyname and esp_transport_ws_init wraps.
  • src/helpers/ESP32TcpBindReuse.cpp: __wrap_tcp_bind sets the option when port == 80, then calls the real tcp_bind. Every other bind passes through unchanged.
  • The pinned AsyncTCP 3.5.0 stays unmodified.

AsyncTCP calls tcp_bind on the lwIP thread (tcpip_api_call), so the wrap takes no lock. Accepted connections inherit the option from the listener.

Testing

  • 745 native tests pass.
  • Builds: heltec_v4_repeater_observer_mqtt, Heltec_v3_repeater_observer_mqtt, Station_G3_ESP32_repeater_observer_mqtt, Heltec_v3_repeater.
  • Linked image checked: AsyncTCP's _tcp_bind_api calls __wrap_tcp_bind.
  • Heltec V4, five MQTT slots: webconfig -> start ota 3 s after the stop -> webconfig -> webconfig -> ota, with curl connections before each stop. Every start served (/ 200, /update 200), no bind error, all five slots stayed connected.

Not tested: V3, Station G3 and Ethernet boards on hardware; a same-day control run on the unfixed build (the failure is from earlier bench sessions).

A closed HTTP connection holds port 80 in TIME_WAIT for 2*MSL,
so the next listen fails with ERR_USE. Set SOF_REUSEADDR on the
new pcb before tcp_bind, and only for port 80, via a linker wrap.
@agessaman
agessaman merged commit 3c65926 into observer-firmware-dev Nov 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant