Skip to content
This repository has been archived by the owner on Apr 27, 2019. It is now read-only.

Fix NullReferenceException when calling Lock.Acquire with CancellationToken #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Consul.Test/LockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void Dispose()
{
m_lock.Dispose();
}

[Fact]
public async Task Lock_AcquireRelease()
{
Expand Down Expand Up @@ -669,5 +669,26 @@ public async Task Lock_DeleteKey()
}
}
}

[Fact]
public async Task Lock_AcquireTimeout()
{
using (var client = new ConsulClient())
{
const string keyName = "test/lock/acquiretimeout";
var distributedLock = await client.AcquireLock(keyName);
try
{
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
{
await Assert.ThrowsAsync<LockNotHeldException>(() => client.AcquireLock(keyName, cts.Token));
}
}
finally
{
await distributedLock.Release();
}
}
}
}
}
12 changes: 11 additions & 1 deletion Consul/Lock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,21 @@ public async Task<CancellationToken> Acquire(CancellationToken ct)
if (ct.IsCancellationRequested || (!IsHeld && !string.IsNullOrEmpty(Opts.Session)))
{
DisposeCancellationTokenSource();
if (_sessionRenewTask != null)
if (_monitorTask != null)
{
try
{
await _monitorTask.ConfigureAwait(false);
}
catch (AggregateException)
{
// Ignore AggregateExceptions from the tasks during Release, since if the Monitor task died, the developer will be Super Confused if they see the exception during Release.
}
}
if (_sessionRenewTask != null)
{
try
{
await _sessionRenewTask.ConfigureAwait(false);
}
catch (AggregateException)
Expand Down