feat: Allow arbitrarily short MQTT.loop() timeouts with socket polling - #259
Open
ide wants to merge 1 commit into
Open
feat: Allow arbitrarily short MQTT.loop() timeouts with socket polling#259ide wants to merge 1 commit into
ide wants to merge 1 commit into
Conversation
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
force-pushed
the
loop-timeout-independent-of-socket-timeout
branch
from
May 23, 2026 05:29
cb787c1 to
28bfda9
Compare
ide
added a commit
to ide/pico-door-sensor
that referenced
this pull request
Jul 12, 2026
Why === Upstream MiniMQTT couples `MQTT.loop()` to the socket timeout: the loop cannot wait less than `socket_timeout` (1 second by default), and newer upstream releases raise `ValueError` when asked to. Lowering the global socket timeout only partly helps because broker connection setup then warns when it takes longer than the socket timeout. So each pass through the main loop could block well past the intended 50 ms before the reed switch was checked again and a door transition published. adafruit/Adafruit_CircuitPython_MiniMQTT#259 (authored from this fork) decouples the loop timeout from the socket timeout using socket readiness polling, so the existing `loop(timeout=0.05)` call genuinely returns within about 50 ms while other network operations keep the full socket timeout. The fork is vendored as compiled .mpy files rather than published as a package or circup bundle: a personal fork awaiting an upstream merge doesn't warrant release infrastructure, and committing the compiled files means deploying needs no compiler toolchain or extra checkout. How === Committed the compiled modules under `vendor/lib/adafruit_minimqtt`, built from commit 28bfda91ad284db04ac91869ced3c6a5aa4517a4 on the fork's `loop-timeout-independent-of-socket-timeout` branch with CircuitPython's mpy-cross 10.2.1. MiniMQTT is pure Python so the .mpy files are bytecode-only and load on any CircuitPython 10.x; the format only changes between major versions. The README records the provenance and how to recompile, including that MicroPython's mpy-cross (such as the Homebrew formula) reports the same mpy version string but emits an incompatible format the device cannot load. Removed `adafruit_minimqtt` from requirements.txt so circup no longer installs the upstream release; kept `adafruit_connection_manager` and `adafruit_ticks`, which MiniMQTT depends on. Extended `scripts/deploy` to copy the vendored .mpy files into the device's `lib/adafruit_minimqtt/`, replacing any circup-installed copy. The main loop's `mqtt.loop(timeout=0.05)` call is unchanged. Test Plan === Ran the forked branch with short loop timeouts on a bench Pico W against the production MQTT broker while developing the upstream PR. Compiled the three modules with the CircuitPython 10.2.1 mpy-cross and confirmed they carry CircuitPython's .mpy magic byte ("C") rather than MicroPython's ("M"). Checked the deploy script with `bash -n` for syntax. To verify after deploying to the garage sensor: watch the boot log over the REPL for a clean connect and discovery publish, confirm `binary_sensor.garage_door` comes back online in Home Assistant, and cycle the door once to confirm state transitions publish promptly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
MQTT.loop()currently requirestimeout >= socket_timeout. For sensor apps this can delay publishing: if a sensor event happens while the app is waiting inMQTT.loop(), the app cannot publish until that wait returns. Lowering the globalsocket_timeoutonly 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.pypython -m tox -e lint -- --all-filesI ran hardware tests and polling benchmarks on a Raspberry Pi Pico 2 W with CircuitPython 10.2.1. The board supports
select.poll()andpoller.ipoll(). Idle polling returned no events as expected.ipoll()was a little faster thanpoll(), used almost no extra heap in the allocation check (this was empirical motivation to supportipollin this PR), and worked in paced 100 Hz and 1000 Hz loops without adding missed periods beyond the baseline.