Skip to content

fix: MQTT callbacks after transport garbage collection - #1240

Open
Carter Tinney (cartertinney) wants to merge 3 commits into
mainfrom
agents/fix-mqtt-callback-gc
Open

fix: MQTT callbacks after transport garbage collection#1240
Carter Tinney (cartertinney) wants to merge 3 commits into
mainfrom
agents/fix-mqtt-callback-gc

Conversation

@cartertinney

Copy link
Copy Markdown
Member

Summary

  • stop Paho's network loop when a callback fires after its owning MQTTTransport has been garbage-collected
  • guard every callback installed by _create_mqtt_client (on_connect, on_disconnect, on_subscribe, on_unsubscribe, on_publish, and on_message) before dereferencing the transport
  • add focused garbage-collection regression coverage, including both successful and failed CONNACK paths and loop-cleanup assertions

Fixes #1192.

This supersedes #1193. That PR guarded only the successful on_connect path, placed its check after the failed-CONNACK branch, and logged that the loop was stopping without calling loop_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 passed
  • python -m pytest tests/unit — 5,443 passed
  • Black 22.3.0 check on changed files
  • Flake8 7.0.0 check on changed files

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>
@cartertinney
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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cartertinney Carter Tinney (cartertinney) changed the title Fix MQTT callbacks after transport garbage collection fix: MQTT callbacks after transport garbage collection Aug 21, 2026
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.

'NoneType' object has no attribute 'on_mqtt_connected_handler'

2 participants