diff --git a/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs b/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs index 854696c73..47a521043 100644 --- a/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs +++ b/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs @@ -58,6 +58,7 @@ public async Task UpdateMessage(string clientId, MqttApplicationMessage applicat { List messagesForSave = null; var saveIsRequired = false; + var hasHandlers = _eventContainer.RetainedMessageChangedEvent.HasHandlers; lock (_messages) { @@ -89,13 +90,13 @@ public async Task UpdateMessage(string clientId, MqttApplicationMessage applicat _logger.Verbose("Client '{0}' set retained message for topic '{1}'.", clientId, applicationMessage.Topic); } - if (saveIsRequired) + if (saveIsRequired && hasHandlers) { messagesForSave = new List(_messages.Values); } } - if (saveIsRequired) + if (saveIsRequired && hasHandlers) { using (await _storageAccessLock.EnterAsync().ConfigureAwait(false)) {