Skip to content

feat: Allow arbitrarily short MQTT.loop() timeouts with socket polling#259

Open
ide wants to merge 1 commit into
adafruit:mainfrom
ide:loop-timeout-independent-of-socket-timeout
Open

feat: Allow arbitrarily short MQTT.loop() timeouts with socket polling#259
ide wants to merge 1 commit into
adafruit:mainfrom
ide:loop-timeout-independent-of-socket-timeout

Conversation

@ide
Copy link
Copy Markdown

@ide ide commented May 22, 2026

Why

MQTT.loop() currently requirestimeout >= socket_timeout. For sensor apps this can delay publishing: if a sensor event happens while the app is waiting in MQTT.loop(), the app cannot publish until that wait returns. Lowering the global socket_timeout only partly helps, because broker connections can then warn when they take longer than the socket timeout to set up.

Ideally MQTT.loop() should be able to check quickly, including with no wait, without changing the socket timeout used by other network operations.

Directly addresses #241, #195, and the short-loop-timeout/socket-timeout subcase in #148.

How

We register connected sockets with select.poll() when available. MQTT.loop() uses readiness polling before reading from the socket, and each poll wait is capped at the earlier of the remaining loop timeout or the next keepalive deadline.

The existing implementation is still used when polling is unavailable depending on the device.

In terms of edge cases, negative loop timeouts are rejected, pollers are cleaned up when sockets close, and poll error events are treated as readable so normal socket handling reports the error.

Test Plan

  • python -m pytest tests/test_loop.py tests/test_reconnect.py
  • python -m tox -e lint -- --all-files

I ran hardware tests and polling benchmarks on a Raspberry Pi Pico 2 W with CircuitPython 10.2.1. The board supports select.poll() and poller.ipoll(). Idle polling returned no events as expected. ipoll() was a little faster than poll(), used almost no extra heap in the allocation check (this was empirical motivation to support ipoll in this PR), and worked in paced 100 Hz and 1000 Hz loops without adding missed periods beyond the baseline.

@ide ide changed the title Allow short MQTT.loop() timeouts with socket polling feat: Allow arbitrarily short MQTT.loop() timeouts with socket polling May 22, 2026
Why: `MQTT.loop()` previously required `timeout >= socket_timeout`. For sensor apps, that can delay publishing: if a sensor event happens while the app is waiting in `MQTT.loop()`, the app cannot publish until that wait returns. Lowering the global `socket_timeout` only partly helps, because broker connections can then warn when they take longer than the shorter timeout. `MQTT.loop()` should be able to check quickly, including with no wait, without changing the socket timeout used by other network operations.

How: We register connected sockets with `select.poll()` when available. `MQTT.loop()` uses readiness polling before reading from the socket, and each poll wait is capped at the earlier of the remaining loop timeout or the next keepalive deadline. The old timeout guard is still used when polling is unavailable. Negative loop timeouts are rejected, pollers are cleaned up when sockets close, and poll error events are treated as readable so normal socket handling reports the error.

Directly addresses adafruit#241, adafruit#195, and the short-loop-timeout/socket-timeout subcase in adafruit#148.

Tests:
- `python -m pytest tests/test_loop.py tests/test_reconnect.py`
- `python -m tox -e lint -- --all-files`
- `git diff --check upstream/main...HEAD`
- Ran hardware polling benchmarks on a Raspberry Pi Pico 2 W with CircuitPython 10.2.1. The board supports `select.poll()` and `poller.ipoll()`. Idle polling returned no events as expected. `ipoll()` was a little faster than `poll()`, used almost no extra heap in the allocation check, and worked in paced 100 Hz and 1000 Hz loops without adding missed periods beyond the baseline.
@ide ide force-pushed the loop-timeout-independent-of-socket-timeout branch from cb787c1 to 28bfda9 Compare May 23, 2026 05:29
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