Skip to content

[udp] Register socket consumption for CONFIG_LWIP_MAX_SOCKETS#14068

Merged
bdraco merged 2 commits intodevfrom
udp-consume-sockets
Feb 19, 2026
Merged

[udp] Register socket consumption for CONFIG_LWIP_MAX_SOCKETS#14068
bdraco merged 2 commits intodevfrom
udp-consume-sockets

Conversation

@bdraco
Copy link
Member

@bdraco bdraco commented Feb 19, 2026

What does this implement/fix?

The UDP component creates up to 2 sockets (broadcast_socket_ + listen_socket_) on ESP32 IDF but did not register them with the socket consumption API. This could cause CONFIG_LWIP_MAX_SOCKETS to be set too low, leading to socket exhaustion at runtime.

Note on SNTP: LWIP has two layers: raw PCBs (udp_new()/udp_sendto(), governed by CONFIG_LWIP_MAX_UDP_PCBS) and the BSD socket API (socket()/bind(), governed by CONFIG_LWIP_MAX_SOCKETS). The SNTP component calls esp_sntp_init() which internally uses a raw UDP PCB, not the socket API, so it does not consume from the socket pool and does not need socket registration.

Note on syslog: The syslog component uses its parent UDP component's socket, so it is already covered by this change.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Developer breaking change (an API change that could break external components)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

Pull request in esphome-docs with documentation (if applicable):

  • N/A (internal fix, no user-facing changes)

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • LN882x
  • nRF52840

Example entry for config.yaml:

# No configuration changes needed - socket consumption is automatic
udp:
  port: 18511

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

  • N/A - no user-facing changes

The UDP component creates up to 2 sockets (broadcast + listen) but
did not register them with the socket consumption API. This could
cause CONFIG_LWIP_MAX_SOCKETS to be set too low on ESP32 IDF.

Closes #14031
Copilot AI review requested due to automatic review settings February 19, 2026 00:28
@bdraco bdraco requested a review from clydebarrow as a code owner February 19, 2026 00:28
@github-actions
Copy link
Contributor

To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file:

external_components:
  - source: github://pr#14068
    components: [udp]
    refresh: 1h

(Added by the PR bot)

@github-actions
Copy link
Contributor

👋 Hi there! I've automatically requested reviews from codeowners based on the files changed in this PR.

@clydebarrow - You've been requested to review this PR as codeowner(s) of 1 file(s) that were modified. Thanks for your time! 🙏

@codecov-commenter
Copy link

codecov-commenter commented Feb 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.11%. Comparing base (bd055e7) to head (2ddf46f).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev   #14068   +/-   ##
=======================================
  Coverage   74.11%   74.11%           
=======================================
  Files          55       55           
  Lines       11589    11589           
  Branches     1578     1578           
=======================================
  Hits         8589     8589           
  Misses       2598     2598           
  Partials      402      402           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdraco bdraco marked this pull request as draft February 19, 2026 00:30
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Registers UDP component socket usage so ESP32 builds can correctly auto-size CONFIG_LWIP_MAX_SOCKETS, preventing runtime socket exhaustion (notably when UDP is used for syslog and similar features).

Changes:

  • Add a config-validation hook to register UDP’s worst-case socket consumption (2 sockets).
  • Wrap udp’s CONFIG_SCHEMA in cv.All(...) to run the socket consumer registration during validation.

@github-actions
Copy link
Contributor

Memory Impact Analysis

Components: udp
Platform: esp8266-ard

Metric Target Branch This PR Change
RAM 29,240 bytes 29,240 bytes ➡️ +0 bytes (0.00%)
Flash 334,687 bytes 334,687 bytes ➡️ +0 bytes (0.00%)

Note: This analysis measures static RAM and Flash usage only (compile-time allocation).
Dynamic memory (heap) cannot be measured automatically.
⚠️ You must test this PR on a real device to measure free heap and ensure no runtime memory issues.

This analysis runs automatically when components change. Memory usage is measured from a representative test configuration.

@bdraco bdraco marked this pull request as ready for review February 19, 2026 00:45
@bdraco
Copy link
Member Author

bdraco commented Feb 19, 2026

Thanks

@bdraco bdraco merged commit 5f82017 into dev Feb 19, 2026
26 checks passed
@bdraco bdraco deleted the udp-consume-sockets branch February 19, 2026 01:01
@swoboda1337 swoboda1337 mentioned this pull request Feb 20, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Feb 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[esp32] Automatic CONFIG_LWIP_MAX_SOCKETS

4 participants