Skip to content

Commit

Permalink
Fixed race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCiliaVincenti committed Dec 30, 2022
1 parent a164747 commit f8727c2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion AsyncKeyedLock/AsyncKeyedLock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageProjectUrl>https://github.com/MarkCiliaVincenti/AsyncKeyedLock</PackageProjectUrl>
<Copyright>MIT</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>6.0.4-rc5</Version>
<Version>6.0.4-rc6</Version>
<PackageIcon>logo.png</PackageIcon>
<PackageReleaseNotes>Optimizations and fixing of more rare race conditions.</PackageReleaseNotes>
<Description>An asynchronous .NET Standard 2.0 library that allows you to lock based on a key (keyed semaphores), limiting concurrent threads sharing the same key to a specified number.</Description>
Expand Down
4 changes: 2 additions & 2 deletions AsyncKeyedLock/AsyncKeyedLockDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public AsyncKeyedLockReleaser<TKey> GetOrAdd(TKey key)
{
if (PoolingEnabled)
{
if (TryGetValue(key, out var releaser) && releaser.TryIncrement())
if (TryGetValue(key, out var releaser) && releaser.TryIncrement(key))
{
return releaser;
}
Expand All @@ -100,7 +100,7 @@ public AsyncKeyedLockReleaser<TKey> GetOrAdd(TKey key)
{
return releaser;
}
if (releaser.TryIncrement())
if (releaser.TryIncrement(key))
{
releaser.IsPooled = true;
_pool.PutObject(releaserToAdd);
Expand Down
4 changes: 2 additions & 2 deletions AsyncKeyedLock/AsyncKeyedLockReleaser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ internal AsyncKeyedLockReleaser(TKey key, SemaphoreSlim semaphoreSlim, AsyncKeye
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool TryIncrement()
internal bool TryIncrement(TKey key)
{
if (Monitor.TryEnter(this))
{
if (IsPooled) // rare race condition
if (IsPooled || !_key.Equals(key)) // rare race condition
{
Monitor.Exit(this);
return false;
Expand Down

0 comments on commit f8727c2

Please sign in to comment.