From 5c40577bec8e42832ef09cae0741ef458f4c6043 Mon Sep 17 00:00:00 2001 From: Paul Fake Date: Tue, 23 Jul 2019 16:20:36 -0400 Subject: [PATCH] Fixed AsyncLock::WaitAsync cancellation bug There isn't any code in MQTTnet that actually uses a cancellation token in WaitAsync, so this is more of a preventative thing than a bug fix. The original code just checks if the task was completed, not whether it was cancelled. If the process is cancelled immediately before the call to WaitAsync, it'll return as normal (https://referencesource.microsoft.com/#mscorlib/system/threading/SemaphoreSlim.cs,612) rather than throw a cancellation exception. This change will ensure we only return the releaser if the wait actually ran to completion rather than exited early due to cancellation. I've tested this, and it properly throws a cancellation exception later on. --- Source/MQTTnet/Internal/AsyncLock.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/MQTTnet/Internal/AsyncLock.cs b/Source/MQTTnet/Internal/AsyncLock.cs index 9b7eefd..17d7404 100644 --- a/Source/MQTTnet/Internal/AsyncLock.cs +++ b/Source/MQTTnet/Internal/AsyncLock.cs @@ -23,7 +23,7 @@ namespace MQTTnet.Internal public Task WaitAsync(CancellationToken cancellationToken) { var task = _semaphore.WaitAsync(cancellationToken); - if (task.IsCompleted) + if (task.Status == TaskStatus.RanToCompletion) { return _releaser; }