Skip to content

Commit

Permalink
Fixes hashicorp#86 NRE in failed lock acquistion with timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
highlyunavailable committed Apr 25, 2017
1 parent 3c6ac45 commit 93f9042
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
19 changes: 19 additions & 0 deletions Consul.Test/LockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,5 +669,24 @@ 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, CancellationToken.None);
try
{
var ts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
await Assert.ThrowsAsync(typeof(LockNotHeldException), () => client.AcquireLock(keyName, ts.Token));
}
finally
{
await distributedLock.Release();
}
}
}
}
}
16 changes: 14 additions & 2 deletions Consul/Lock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,28 @@ 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 task, since if the
// task died, we don't care any more.
}
}
if (_sessionRenewTask != null)
{
try
{
await _sessionRenewTask.ConfigureAwait(false);
}
catch (AggregateException)
{
// Ignore AggregateExceptions from the tasks during Release, since if the Renew task died, the developer will be Super Confused if they see the exception during Release.
// Ignore AggregateExceptions from the task, since if the
// task died, we don't care any more.
}
}
}
Expand Down

0 comments on commit 93f9042

Please sign in to comment.