Skip to content

Commit

Permalink
attempt to prevent disposed exception on mqtt client
Browse files Browse the repository at this point in the history
  • Loading branch information
amadeo-alex committed Oct 12, 2024
1 parent 5fc4112 commit af218bd
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal static async Task ShutdownAsync(TimeSpan waitBeforeClosing)

// stop mqtt
await Variables.MqttManager.AnnounceAvailabilityAsync(true);
Variables.MqttManager.Disconnect();
await Variables.MqttManager.DisconnectAsync();

// stop rpc
Variables.RpcServer?.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,14 +520,14 @@ public async Task ClearDeviceConfigAsync()
/// <summary>
/// Disconnect from the broker (if connected)
/// </summary>
public void Disconnect()
public async Task DisconnectAsync()
{
if (_mqttClient == null)
return;

if (IsConnected())
{
_mqttClient.InternalClient.DisconnectAsync();
await _mqttClient.StopAsync();
_mqttClient.Dispose();
}

Expand Down
2 changes: 1 addition & 1 deletion src/HASS.Agent/HASS.Agent.Shared/Mqtt/IMqttManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface IMqttManager
MqttStatus GetStatus();
Task AnnounceAvailabilityAsync(bool offline = false);
Task ClearDeviceConfigAsync();
void Disconnect();
Task DisconnectAsync();
Task SubscribeAsync(AbstractCommand command);
Task UnsubscribeAsync(AbstractCommand command);

Expand Down
2 changes: 1 addition & 1 deletion src/HASS.Agent/HASS.Agent/Forms/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private async void ProcessChanges()
await Task.Delay(250);

// disconnect mqtt so we don't get announced again
await Task.Run(Variables.MqttManager.Disconnect);
await Variables.MqttManager.DisconnectAsync();

forceRestart = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/HASS.Agent/HASS.Agent/Functions/HelperFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ internal static async Task ShutdownAsync(TimeSpan waitBeforeClosing)
{
// stop mqtt
await Variables.MqttManager.AnnounceAvailabilityAsync(true);
Variables.MqttManager.Disconnect();
await Variables.MqttManager.DisconnectAsync();

// remove tray icon
Variables.MainForm?.HideTrayIcon();
Expand Down
9 changes: 6 additions & 3 deletions src/HASS.Agent/HASS.Agent/MQTT/MqttManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -611,15 +611,18 @@ public async Task ClearDeviceConfigAsync()
/// <summary>
/// Disconnect from the broker (if connected)
/// </summary>
public void Disconnect()
public async Task DisconnectAsync()
{
if (!Variables.AppSettings.MqttEnabled)
return;

if (_mqttClient == null)
return;

if (IsConnected())
{
_mqttClient?.InternalClient?.DisconnectAsync();
_mqttClient?.Dispose();
await _mqttClient.StopAsync();
_mqttClient.Dispose();
}

Log.Information("[MQTT] Disconnected");
Expand Down

0 comments on commit af218bd

Please sign in to comment.