fix(mqtt_publisher): pass tls_insecure=None for non-TLS connections#465
Open
csutcliff wants to merge 1 commit into
Open
fix(mqtt_publisher): pass tls_insecure=None for non-TLS connections#465csutcliff wants to merge 1 commit into
csutcliff wants to merge 1 commit into
Conversation
aiomqtt forwards any non-None tls_insecure value to paho's tls_insecure_set(), which raises "Must configure SSL context before using tls_insecure_set" when no SSL context is configured. That ValueError isn't caught by the reconnect loop, so the connect task dies silently and connect() waits forever on the connected event: with a plain tcp:// broker URI the gateway logs only "MQTT client disconnected" and hangs at startup without ever reaching the broker. Only pass a value when TLS is enabled with hostname checking disabled; otherwise leave it None so aiomqtt skips tls_insecure_set() entirely.
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.
Problem
Since the aiomqtt migration (#457), the gateway hangs at startup when configured with a plain
tcp://broker URI. It logsand then does nothing forever: no connection ever reaches the broker, no error is logged, no retry happens, and the process stays alive. 0.12.0 works with the same configuration.
Root cause
MqttPublisher.__run_looppassestls_insecure=bool(ssl_context and ...)toaiomqtt.Client. Without TLS that evaluates toFalserather thanNone, and aiomqtt applies any non-None value via paho'stls_insecure_set(), which raises:The ValueError isn't one of the exceptions the reconnect loop handles (
MqttConnectError/MqttError/CancelledError), so the connect task dies with only thefinallyblock's "MQTT client disconnected" log, the connected event is never set, andconnect()waits on it forever.Fix
Only pass a
tls_insecurevalue when TLS is enabled with hostname checking disabled; otherwise leave itNoneso aiomqtt skipstls_insecure_set()entirely. Semantics for the TLS cases are unchanged.Added regression tests covering the three combinations (plain TCP →
None, TLS without hostname check →True, TLS with hostname check →None); the plain-TCP and TLS-with-check cases fail against currentdevelop.mypy,ruff check,ruff format --checkandpytestall pass.