Repository: blakeblackshear/frigate

Issue:
[Support]: mqtt sometimes fails
### Describe the problem you are having

Sometimes on boot mqtt fails with the following log.
After several reboots it will run just fine

### Version

0.13.1-34fb1c2

### Frigate config file

```yaml
mqtt:
  host: 192.168.86.13
  user: *
  password: *
```


### Relevant log output

```shell
2024-02-03 11:29:28.183720955  [INFO] Preparing Frigate...
2024-02-03 11:29:30.429983288  [INFO] Starting Frigate...
2024-02-03 11:29:43.565715210  [2024-02-03 11:29:43] frigate.app                    INFO    : Starting Frigate (0.13.1-34fb1c2)
2024-02-03 11:29:43.668674989  [2024-02-03 11:29:43] frigate.config                 WARNING : Customizing more than a detector model path is unsupported.
2024-02-03 11:29:43.693848249  [2024-02-03 11:29:43] peewee_migrate.logs            INFO    : Starting migrations
2024-02-03 11:29:43.786091779  [2024-02-03 11:29:43] peewee_migrate.logs            INFO    : There is nothing to migrate
2024-02-03 11:29:44.077802695  [2024-02-03 11:29:44] frigate.app                    INFO    : Recording process started: 465
2024-02-03 11:29:44.082183481  [2024-02-03 11:29:44] frigate.app                    INFO    : go2rtc process pid: 89
2024-02-03 11:29:44.092318107  Exception in thread mqtt:
2024-02-03 11:29:44.092347726  Traceback (most recent call last):
2024-02-03 11:29:44.092350843    File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
2024-02-03 11:29:44.132000773      self.run()
2024-02-03 11:29:44.132008391    File "/usr/lib/python3.9/threading.py", line 892, in run
2024-02-03 11:29:44.132351154      self._target(*self._args, **self._kwargs)
2024-02-03 11:29:44.132356410    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 3591, in _thread_main
2024-02-03 11:29:44.166476612      self.loop_forever(retry_first_connection=True)
2024-02-03 11:29:44.166483467    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 1756, in loop_forever
2024-02-03 11:29:44.167015600      rc = self._loop(timeout)
2024-02-03 11:29:44.167020085    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 1164, in _loop
2024-02-03 11:29:44.167335718      rc = self.loop_read()
2024-02-03 11:29:44.167339384    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 1556, in loop_read
2024-02-03 11:29:44.167728400      rc = self._packet_read()
2024-02-03 11:29:44.167732996    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 2439, in _packet_read
2024-02-03 11:29:44.168284999      rc = self._packet_handle()
2024-02-03 11:29:44.168305384    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 3033, in _packet_handle
2024-02-03 11:29:44.169013241      return self._handle_publish()
2024-02-03 11:29:44.169016831    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 3327, in _handle_publish
2024-02-03 11:29:44.169718804      self._handle_on_message(message)
2024-02-03 11:29:44.169722152    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 3556, in _handle_on_message
2024-02-03 11:29:44.170469665      callback(self, self._userdata, message)
2024-02-03 11:29:44.170472610    File "/opt/frigate/frigate/comms/mqtt.py", line 110, in on_mqtt_command
2024-02-03 11:29:44.177426729      self._dispatcher(
2024-02-03 11:29:44.177430972    File "/opt/frigate/frigate/comms/dispatcher.py", line 80, in _receive
2024-02-03 11:29:44.179490027      self._camera_settings_handlers[command](camera_name, payload)
2024-02-03 11:29:44.179512034    File "/opt/frigate/frigate/comms/dispatcher.py", line 281, in _on_recordings_command
2024-02-03 11:29:44.179681083      self.publish(f"{camera_name}/recordings/state", payload, retain=True)
2024-02-03 11:29:44.179685405    File "/opt/frigate/frigate/comms/dispatcher.py", line 111, in publish
2024-02-03 11:29:44.179791262      comm.publish(topic, payload, retain)
2024-02-03 11:29:44.179795488    File "/opt/frigate/frigate/comms/ws.py", line 102, in publish
2024-02-03 11:29:44.180243298      self.websocket_server.manager.broadcast(ws_message)
2024-02-03 11:29:44.180247725  AttributeError: 'WebSocketClient' object has no attribute 'websocket_server'
2024-02-03 11:29:44.244791022  [2024-02-03 11:29:44] frigate.app                    INFO    : Output process started: 476
```


### FFprobe output from your camera

```shell
[{"return_code":0,"stderr":"","stdout":{"programs":[],"streams":[{"avg_frame_rate":"30/1","codec_long_name":"H.264/AVC/MPEG-4AVC/MPEG-4part10","height":1440,"width":2560},{"avg_frame_rate":"0/0","codec_long_name":"AAC(AdvancedAudioCoding)"}]}},{"return_code":0,"stderr":"","stdout":{"programs":[],"streams":[{"avg_frame_rate":"20/1","codec_long_name":"H.264/AVC/MPEG-4AVC/MPEG-4part10","height":672,"width":896},{"avg_frame_rate":"0/0","codec_long_name":"AAC(AdvancedAudioCoding)"}]}}]
```


### Frigate stats

_No response_

### Operating system

Debian

### Install method

Docker Compose

### Coral version

CPU (no coral)

### Network connection

Wired

### Camera make and model

Reolink

### Any other information that may be helpful

_No response_


Discussion:
What MQTT server are you using 
Seeing the same issue after upgrading frigate to 0.13. I'm using `eclipse-mosquitto:latest`.

Other services that also use this mqtt service are not having any connectivity issues like this.

Resolving patch:
diff --git a/frigate/comms/ws.py b/frigate/comms/ws.py
--- a/frigate/comms/ws.py
+++ b/frigate/comms/ws.py
@@ -38,6 +38,7 @@ class WebSocketClient(Communicator):  # type: ignore[misc]
 
     def __init__(self, config: FrigateConfig) -> None:
         self.config = config
+        self.websocket_server = None
 
     def subscribe(self, receiver: Callable) -> None:
         self._dispatcher = receiver
@@ -98,6 +99,10 @@ def publish(self, topic: str, payload: str, _: bool) -> None:
             logger.debug(f"payload for {topic} wasn't text. Skipping...")
             return
 
+        if self.websocket_server is None:
+            logger.debug("Skipping message, websocket not connected yet")
+            return
+
         try:
             self.websocket_server.manager.broadcast(ws_message)
         except ConnectionResetError:


Test patch: