-
Notifications
You must be signed in to change notification settings - Fork 10
Concurrency Warning #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Concurrency Warning #288
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dba4ab4
Remove broken deadlock awareness
nick-roscarel-octo 2e8befa
Remove unnecessary task/thread monitoring
nick-roscarel-octo 513129b
Merge branch 'refs/heads/master' into nick/deadlock-aware-lock-logging
nick-roscarel-octo 7799950
Part-way implement concurrency handlers
nick-roscarel-octo eaccf7f
Clean up and tests
YuKitsune 598df6f
Moving things around
YuKitsune 2e330d9
Docs
YuKitsune 43c4b4c
Clean up
YuKitsune 336e820
Feedback clean up
YuKitsune 7e6d058
Make implementations private
YuKitsune 9d7b661
Refactor thread-safe enumerables
YuKitsune 596a0b0
Spike: Include the stacktrace in the log
YuKitsune File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
source/Nevermore/Advanced/AsyncEnumerableWithConcurrencyHandling.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Nevermore.Advanced.Concurrency; | ||
|
|
||
| namespace Nevermore.Advanced | ||
| { | ||
| internal class AsyncEnumerableWithConcurrencyHandling<T> : IAsyncEnumerable<T> | ||
| { | ||
| readonly Func<IAsyncEnumerable<T>> innerFunc; | ||
| readonly ITransactionConcurrencyHandler transactionConcurrencyHandler; | ||
|
|
||
| public AsyncEnumerableWithConcurrencyHandling(IAsyncEnumerable<T> inner, ITransactionConcurrencyHandler transactionConcurrencyHandler) | ||
| : this(() => inner, transactionConcurrencyHandler) | ||
| { | ||
| } | ||
|
|
||
| public AsyncEnumerableWithConcurrencyHandling(Func<IAsyncEnumerable<T>> innerFunc, ITransactionConcurrencyHandler transactionConcurrencyHandler) | ||
| { | ||
| this.innerFunc = innerFunc; | ||
| this.transactionConcurrencyHandler = transactionConcurrencyHandler; | ||
| } | ||
|
|
||
| public async IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = new()) | ||
| { | ||
| using var mutex = await transactionConcurrencyHandler.LockAsync(cancellationToken).ConfigureAwait(false); | ||
| var inner = innerFunc(); | ||
| await foreach (var item in inner.WithCancellation(cancellationToken).ConfigureAwait(false)) yield return item; | ||
| } | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
source/Nevermore/Advanced/Concurrency/ITransactionConcurrencyHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Nevermore.Advanced.Concurrency | ||
| { | ||
| public interface ITransactionConcurrencyHandler : IDisposable | ||
| { | ||
| IDisposable Lock(); | ||
|
|
||
| Task<IDisposable> LockAsync(CancellationToken cancellationToken); | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
source/Nevermore/Advanced/Concurrency/LockOnlyConcurrencyHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Nito.AsyncEx; | ||
|
|
||
| namespace Nevermore.Advanced.Concurrency | ||
| { | ||
| class LockOnlyConcurrencyHandler : ITransactionConcurrencyHandler | ||
| { | ||
| readonly SemaphoreSlim semaphore = new(1, 1); | ||
|
|
||
| public IDisposable Lock() | ||
| { | ||
| return semaphore.Lock(); | ||
| } | ||
|
|
||
| public async Task<IDisposable> LockAsync(CancellationToken cancellationToken) | ||
| { | ||
| return await semaphore.LockAsync(cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| semaphore.Dispose(); | ||
| } | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
source/Nevermore/Advanced/Concurrency/LockWithLoggingConcurrencyHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Nevermore.Diagnositcs; | ||
| using Nito.AsyncEx; | ||
|
|
||
| namespace Nevermore.Advanced.Concurrency | ||
| { | ||
| class LockWithLoggingConcurrencyHandler : ITransactionConcurrencyHandler | ||
| { | ||
| static readonly ILog Log = LogProvider.For<LockWithLoggingConcurrencyHandler>(); | ||
|
|
||
| readonly SemaphoreSlim semaphore = new(1, 1); | ||
|
|
||
| public IDisposable Lock() | ||
| { | ||
| // `SemaphoreSlim` counts down, so if it's 0 then there's a concurrent execution happening. | ||
| if (semaphore.CurrentCount == 0) | ||
| { | ||
| Log.WarnFormat("Concurrent query execution detected. Stacktrace: {0}", Environment.StackTrace); | ||
| } | ||
|
|
||
| return semaphore.Lock(); | ||
| } | ||
|
|
||
| public async Task<IDisposable> LockAsync(CancellationToken cancellationToken) | ||
| { | ||
| // `SemaphoreSlim` counts down, so if it's 0 then there's a concurrent execution happening. | ||
| if (semaphore.CurrentCount == 0) | ||
| { | ||
| Log.WarnFormat("Concurrent query execution detected. Stacktrace: {0}", Environment.StackTrace); | ||
| } | ||
|
|
||
| return await semaphore.LockAsync(cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| semaphore.Dispose(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We decided to replace the
DeadlockAwareLockwith this abstraction as a way for us to enable logging without locking. It's a bit much, but felt cleaner than introducing a bunch of additional checks into a single class.Open to alternatives.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a spike of what this would look like if we had a single type: #289