Improve allocations for AsyncSignal.

This commit is contained in:
Christian
2022-11-26 14:45:34 +01:00
parent c45cb8b04e
commit 581a024ceb
3 changed files with 24 additions and 2 deletions
+1 -1
View File
@@ -1 +1 @@
[Core] Fixed a memory leak in _AsyncSignal_ implementation (#1586, thanks to @mario-zelger).
+21
View File
@@ -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
{