Also cleanup MqttClientSessionsManager._subscriberSessions when session is taken over by a new one with CleanSession=true, fix #1552 (#1553)

This commit is contained in:
SilverFox
2022-11-06 10:35:15 +01:00
committed by GitHub
parent 2842f27522
commit 5b1cc7d713
2 changed files with 9 additions and 3 deletions
@@ -90,7 +90,7 @@ namespace MQTTnet.Tests.Internal
Assert.IsTrue(stopwatch.ElapsedMilliseconds > 900);
}
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public async Task Fail_For_Two_Waiters()
@@ -485,9 +485,10 @@ namespace MQTTnet.Server
using (await _createConnectionSyncRoot.EnterAsync().ConfigureAwait(false))
{
MqttSession session;
MqttSession oldSession;
lock (_sessionsManagementLock)
{
if (!_sessions.TryGetValue(connectPacket.ClientId, out session))
if (!_sessions.TryGetValue(connectPacket.ClientId, out oldSession))
{
session = CreateSession(connectPacket.ClientId, validatingConnectionEventArgs.SessionItems, sessionShouldPersist);
}
@@ -496,11 +497,14 @@ namespace MQTTnet.Server
if (connectPacket.CleanSession)
{
_logger.Verbose("Deleting existing session of client '{0}' due to clean start.", connectPacket.ClientId);
_subscriberSessions.Remove(oldSession);
session = CreateSession(connectPacket.ClientId, validatingConnectionEventArgs.SessionItems, sessionShouldPersist);
}
else
{
_logger.Verbose("Reusing existing session of client '{0}'.", connectPacket.ClientId);
session = oldSession;
oldSession = null;
// Session persistence could change for MQTT 5 clients that reconnect with different SessionExpiryInterval
session.IsPersistent = sessionShouldPersist;
connAckPacket.IsSessionPresent = true;
@@ -533,7 +537,7 @@ namespace MQTTnet.Server
existingClient.IsTakenOver = true;
await existingClient.StopAsync(MqttDisconnectReasonCode.SessionTakenOver).ConfigureAwait(false);
if (_eventContainer.ClientConnectedEvent.HasHandlers)
if (_eventContainer.ClientDisconnectedEvent.HasHandlers)
{
var eventArgs = new ClientDisconnectedEventArgs(
existingClient.Id,
@@ -544,6 +548,8 @@ namespace MQTTnet.Server
await _eventContainer.ClientDisconnectedEvent.InvokeAsync(eventArgs).ConfigureAwait(false);
}
}
oldSession?.Dispose();
}
return client;