fix: MQTT callbacks after transport garbage collection - #1240
Open
Carter Tinney (cartertinney) wants to merge 3 commits into
Open
fix: MQTT callbacks after transport garbage collection#1240Carter Tinney (cartertinney) wants to merge 3 commits into
Carter Tinney (cartertinney) wants to merge 3 commits into
Conversation
Stop Paho's network loop and return safely when any MQTT callback runs after its transport has been garbage-collected. Cover successful and failed connects plus disconnect, subscribe, unsubscribe, publish, and message callbacks. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Carter Tinney (cartertinney)
marked this pull request as draft
August 21, 2026 23:28
Make it explicit that the callback helper obtains the transport by dereferencing its weak reference before deciding whether to stop Paho's loop. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Carter Tinney (cartertinney)
marked this pull request as ready for review
August 21, 2026 23:40
Carter Tinney (cartertinney)
requested review from
Ewerton Scaboro da Silva (ewertons) and
olivakar
and
a balanced review from Copilot
August 21, 2026 23:40
Copilot started reviewing on behalf of
Carter Tinney (cartertinney)
August 21, 2026 23:46
View session
There was a problem hiding this comment.
Pull request overview
Prevents MQTT callback failures after their owning MQTTTransport is garbage-collected.
Changes:
- Centralizes weak-reference validation and Paho loop shutdown.
- Guards all six MQTT callbacks.
- Adds garbage-collection regression tests.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
azure-iot-device/azure/iot/device/common/mqtt_transport.py |
Safely stops callbacks after transport collection. |
tests/unit/common/test_mqtt_transport.py |
Covers callback cleanup across all callback types. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
3 tasks
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.
Summary
MQTTTransporthas been garbage-collected_create_mqtt_client(on_connect,on_disconnect,on_subscribe,on_unsubscribe,on_publish, andon_message) before dereferencing the transportFixes #1192.
This supersedes #1193. That PR guarded only the successful
on_connectpath, placed its check after the failed-CONNACK branch, and logged that the loop was stopping without callingloop_stop(). This change uses one cleanup path for all six callbacks, actually stops the Paho loop, and preserves propagation of cleanup errors.Validation
python -m pytest tests/unit/common/test_mqtt_transport.py— 281 passedpython -m pytest tests/unit— 5,443 passed