mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 03:24:33 +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
|
// ReSharper disable once FieldCanBeMadeReadOnly.Local
|
||||||
CancellationTokenRegistration _cancellationTokenRegistration;
|
CancellationTokenRegistration _cancellationTokenRegistration;
|
||||||
|
|
||||||
|
volatile bool _isCompleted;
|
||||||
|
|
||||||
public AsyncSignalWaiter(CancellationToken cancellationToken)
|
public AsyncSignalWaiter(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (cancellationToken.CanBeCanceled)
|
if (cancellationToken.CanBeCanceled)
|
||||||
@@ -115,6 +117,12 @@ namespace MQTTnet.Internal
|
|||||||
|
|
||||||
public void Approve()
|
public void Approve()
|
||||||
{
|
{
|
||||||
|
if (_isCompleted)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_isCompleted = true;
|
||||||
_promise.TrySetResult(true);
|
_promise.TrySetResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,11 +130,24 @@ namespace MQTTnet.Internal
|
|||||||
{
|
{
|
||||||
_cancellationTokenRegistration.Dispose();
|
_cancellationTokenRegistration.Dispose();
|
||||||
|
|
||||||
|
if (_isCompleted)
|
||||||
|
{
|
||||||
|
// Avoid allocation of _ObjectDisposedException_ which may not be used.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_isCompleted = true;
|
||||||
_promise.TrySetException(new ObjectDisposedException(nameof(AsyncSignalWaiter)));
|
_promise.TrySetException(new ObjectDisposedException(nameof(AsyncSignalWaiter)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cancel()
|
void Cancel()
|
||||||
{
|
{
|
||||||
|
if (_isCompleted)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_isCompleted = true;
|
||||||
_promise.TrySetCanceled();
|
_promise.TrySetCanceled();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace MQTTnet.Server
|
namespace MQTTnet.Server
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user