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

@ide ide commented May 22, 2026

Copy link
Copy Markdown

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
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.
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