diff --git a/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs b/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs index 8995b35f..6d2e288f 100644 --- a/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs +++ b/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs @@ -62,6 +62,7 @@ public async Task UpdateMessage(string clientId, MqttApplicationMessage applicat { List messagesForSave = null; var saveIsRequired = false; + var hasHandlers = _eventContainer.RetainedMessageChangedEvent.HasHandlers; lock (_messages) { @@ -93,13 +94,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)) {