mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
Improve allocations for AsyncSignal.
This commit is contained in:
@@ -1 +1 @@
|
||||
|
||||
[Core] Fixed a memory leak in _AsyncSignal_ implementation (#1586, thanks to @mario-zelger).
|
||||
|
||||
@@ -103,6 +103,8 @@ namespace MQTTnet.Internal
|
||||
// ReSharper disable once FieldCanBeMadeReadOnly.Local
|
||||
CancellationTokenRegistration _cancellationTokenRegistration;
|
||||
|
||||
volatile bool _isCompleted;
|
||||
|
||||
public AsyncSignalWaiter(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
@@ -115,6 +117,12 @@ namespace MQTTnet.Internal
|
||||
|
||||
public void Approve()
|
||||
{
|
||||
if (_isCompleted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isCompleted = true;
|
||||
_promise.TrySetResult(true);
|
||||
}
|
||||
|
||||
@@ -122,11 +130,24 @@ namespace MQTTnet.Internal
|
||||
{
|
||||
_cancellationTokenRegistration.Dispose();
|
||||
|
||||
if (_isCompleted)
|
||||
{
|
||||
// Avoid allocation of _ObjectDisposedException_ which may not be used.
|
||||
return;
|
||||
}
|
||||
|
||||
_isCompleted = true;
|
||||
_promise.TrySetException(new ObjectDisposedException(nameof(AsyncSignalWaiter)));
|
||||
}
|
||||
|
||||
void Cancel()
|
||||
{
|
||||
if (_isCompleted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isCompleted = true;
|
||||
_promise.TrySetCanceled();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace MQTTnet.Server
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user