mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
Also cleanup MqttClientSessionsManager._subscriberSessions when session is taken over by a new one with CleanSession=true, fix #1552 (#1553)
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user