[PR #328] [MERGED] Shut down MQTT relay and smart plug services explicitly #998

Closed
opened 2026-05-06 12:34:24 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/maziggy/bambuddy/pull/328
Author: @jeffsf
Created: 2/10/2026
Status: Merged
Merged: 2/13/2026
Merged by: @maziggy

Base: 0.2.0bHead: fixes/327


📝 Commits (3)

  • c45568b Shut down MQTT relay and smart plug services explicitly
  • 2af5289 Merge branch '0.2.0b' into fixes/327
  • 7c36283 Merge branch '0.2.0b' into fixes/327

📊 Changes

5 files changed (+32 additions, -10 deletions)

View changed files

📝 backend/app/main.py (+5 -0)
📝 backend/app/services/bambu_mqtt.py (+8 -2)
📝 backend/app/services/mqtt_relay.py (+7 -2)
📝 backend/app/services/mqtt_smart_plug.py (+8 -2)
📝 backend/app/services/printer_manager.py (+4 -4)

📄 Description

Existing code did not allow the MQTT relay to be shut down gracefully.

Moved _client.loop_stop() after _client.disconnect() call to allow Paho to be notified to disconnect and shut down without reconnecting.

Added _client._disconnection_event to help prevent a race condition where a disconnection is requested but the loop is immediately stopped. Modify _client.disconnect() to take a timeout parameter (default: 0). Wait in that routine for the disconnection event with the given timeout.

Apply same logic to MQTTSmartPlugService and add to shutdown sequence.

Shutdown the MQTT relay last in lifespan() with a hard-coded 2 sec timeout.

The disconnect timeout is also available to printer MQTT clients. It is left to the default of 0 at this time. If later enabled, it should be a configuration parameters, especially for large farms.

Fixes: #327

Description

  • Reorder MQTT disconnect and loop_stop
  • Explicitly stop MQTTSmartPlugService on graceful shutdown
  • Try to prevent potential race condition on disconnect and loop_stop

Fixes #327

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Code refactoring
  • Performance improvement
  • Test addition or update

Changes Made

See commit message or above description

Screenshots

Testing

  • I have tested this on my local machine (macOS 26)
  • I have tested with my printer model: A1

Checklist

  • My code follows the project's coding style
  • I have commented my code where necessary
  • I have updated the documentation (if needed)
  • My changes generate no new warnings
  • I have tested my changes thoroughly

Additional Notes


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/maziggy/bambuddy/pull/328 **Author:** [@jeffsf](https://github.com/jeffsf) **Created:** 2/10/2026 **Status:** ✅ Merged **Merged:** 2/13/2026 **Merged by:** [@maziggy](https://github.com/maziggy) **Base:** `0.2.0b` ← **Head:** `fixes/327` --- ### 📝 Commits (3) - [`c45568b`](https://github.com/maziggy/bambuddy/commit/c45568bc7e54042d6af4f4aa479301cda4c6b9f2) Shut down MQTT relay and smart plug services explicitly - [`2af5289`](https://github.com/maziggy/bambuddy/commit/2af52897366023cb93bfa6985d26c6288da2a850) Merge branch '0.2.0b' into fixes/327 - [`7c36283`](https://github.com/maziggy/bambuddy/commit/7c362836659abbcc51d937dda62f0b1e342492e3) Merge branch '0.2.0b' into fixes/327 ### 📊 Changes **5 files changed** (+32 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `backend/app/main.py` (+5 -0) 📝 `backend/app/services/bambu_mqtt.py` (+8 -2) 📝 `backend/app/services/mqtt_relay.py` (+7 -2) 📝 `backend/app/services/mqtt_smart_plug.py` (+8 -2) 📝 `backend/app/services/printer_manager.py` (+4 -4) </details> ### 📄 Description Existing code did not allow the MQTT relay to be shut down gracefully. Moved _client.loop_stop() after _client.disconnect() call to allow Paho to be notified to disconnect and shut down without reconnecting. Added _client._disconnection_event to help prevent a race condition where a disconnection is requested but the loop is immediately stopped. Modify _client.disconnect() to take a timeout parameter (default: 0). Wait in that routine for the disconnection event with the given timeout. Apply same logic to MQTTSmartPlugService and add to shutdown sequence. Shutdown the MQTT relay last in lifespan() with a hard-coded 2 sec timeout. The disconnect timeout is also available to printer MQTT clients. It is left to the default of 0 at this time. If later enabled, it should be a configuration parameters, especially for large farms. Fixes: #327 ## Description - Reorder MQTT disconnect and loop_stop - Explicitly stop MQTTSmartPlugService on graceful shutdown - Try to prevent potential race condition on disconnect and loop_stop ## Related Issue <!-- Link to the issue this PR addresses (if applicable) --> Fixes #327 ## Type of Change <!-- Mark the relevant option with an "x" --> - [X] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Code refactoring - [ ] Performance improvement - [ ] Test addition or update ## Changes Made See commit message or above description ## Screenshots <!-- If applicable, add screenshots to demonstrate your changes --> ## Testing <!-- Describe how you tested your changes --> - [X] I have tested this on my local machine (macOS 26) - [X] I have tested with my printer model: A1 ## Checklist - [X] My code follows the project's coding style - [X] I have commented my code where necessary - [X] I have updated the documentation (if needed) - [X] My changes generate no new warnings - [X] I have tested my changes thoroughly ## Additional Notes <!-- Add any additional information that reviewers should know --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 12:34:24 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/bambuddy#998
No description provided.