Skip to content

🐛 [TRIGGER] Prevent MQTT trigger from crashing process when broker is disconnected (fixes #1520) - #1528

Merged
fmartinou merged 1 commit into
mainfrom
fix/1520_mqtt_disconnect_crash
Nov 6, 2026
Merged

fmartinou merged 1 commit into
mainfrom
fix/1520_mqtt_disconnect_crash

Conversation

@fmartinou

Copy link
Copy Markdown
Collaborator

Summary of Changes

When a container hosting the MQTT broker is updated (for example, by the dockercompose trigger), the broker container stops and is temporarily removed before being recreated. During this window, the Docker watcher detects container lifecycle changes and emits containerAdded / containerUpdated events.

Previously, Mqtt.ts registered event listeners calling this.trigger(container) directly without catching rejected promises. Because the MQTT client is disconnected during broker restart (this.client.connected === false), Mqtt.trigger() threw Error('MQTT client is not connected'). In Node.js, an unhandled rejection in an EventEmitter listener terminates the process, causing WUD to crash and leaving the broker container stopped and removed before the update flow could complete.

Fix Details

  1. MQTT Event Listeners:
    • Wrapped onContainerAdded and onContainerUpdated in Mqtt.ts in try ... catch blocks that log a warning and debug information without letting unhandled promise rejections escape.
    • Cleanly unregister the event listeners on Mqtt.deregisterComponent().
  2. Home Assistant Event & Command Handlers:
    • Wrapped event listeners and MQTT message callbacks in Hass.ts with try ... catch blocks to avoid unhandled rejections during broker disconnections.
  3. Trigger Base Hardening:
    • Hardened handleContainerReport, handleContainerReports, handleContainerUpdatePre, handleContainerPreload, and listener registrations in Trigger.ts to ensure top-level exception handling.
    • Replaced loose e: any catch blocks with strict TypeScript e: unknown.
  4. Unit Tests:
    • Added unit tests in app/triggers/providers/mqtt/Mqtt.test.ts verifying that containerAdded and containerUpdated handle disconnected client scenarios gracefully without throwing unhandled rejections.
    • Added unit test verifying that deregisterComponent properly unregisters container listeners.
  5. Changelog:
    • Documented the fix in website/docs/changelog/next.md under Bug Fixes, maintaining strict categorization and alphabetical sorting.

Fixes #1520

@fmartinou
fmartinou merged commit ea37257 into main Nov 6, 2026
20 of 22 checks passed
@fmartinou
fmartinou deleted the fix/1520_mqtt_disconnect_crash branch November 6, 2026 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant