Repository navigation
Preserve DI scope ownership and synchronize concurrent property registration #7476
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bfb9d9f
Fix DI row scope ownership and concurrent property registration (#7472)
thomhurst 4e1009f
Synchronize concurrent cleanup handler merges (#7472)
thomhurst e167fcf
Preserve row context identities and session scope cleanup (#7472)
thomhurst 67737e2
Preserve factory state for temporary constructors (#7472)
thomhurst d7f5389
Fix reflection static scope cleanup and concurrent handler merging (#…
thomhurst 5b92436
Fix netstandard event lock nullability for #7472
thomhurst 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 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 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,119 @@ | ||
| namespace TUnit.Core; | ||
|
|
||
| /// <summary> | ||
| /// Owns a row's scopes. Object lookup releases each scope at its first resolved object's graph | ||
| /// depth; completion releases scopes left by failed factories or untracked values. | ||
| /// </summary> | ||
| internal sealed class DataRowScopeCollection | ||
| { | ||
| private readonly object _lock = new(); | ||
| private readonly List<ScopeOwner> _owners = []; | ||
| private readonly Dictionary<object, List<ScopeOwner>> _objects = new(Helpers.ReferenceEqualityComparer.Instance); | ||
|
|
||
| internal ScopeOwner Register(object? scope) | ||
| { | ||
| var owner = new ScopeOwner(scope); | ||
| lock (_lock) | ||
| _owners.Add(owner); | ||
| return owner; | ||
| } | ||
|
|
||
| internal void RegisterObject(ScopeOwner owner, object? instance) | ||
| { | ||
| if (instance is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| lock (_lock) | ||
| { | ||
| if (!_objects.TryGetValue(instance, out var owners)) | ||
| _objects.Add(instance, owners = []); | ||
| if (!owners.Contains(owner)) | ||
| owners.Add(owner); | ||
| } | ||
| } | ||
|
|
||
| internal DataRowScopeCollection Clone() | ||
| { | ||
| var clone = new DataRowScopeCollection(); | ||
| clone.AddRange(this); | ||
| return clone; | ||
| } | ||
|
|
||
| internal void AddRange(DataRowScopeCollection scopes) | ||
| { | ||
| // Snapshot the source before locking the destination to avoid lock-order inversions. | ||
| ScopeOwner[] owners; | ||
| KeyValuePair<object, ScopeOwner[]>[] objects; | ||
| lock (scopes._lock) | ||
| { | ||
| owners = scopes._owners.ToArray(); | ||
| objects = scopes._objects.Select(pair => | ||
| new KeyValuePair<object, ScopeOwner[]>(pair.Key, pair.Value.ToArray())).ToArray(); | ||
| } | ||
| lock (_lock) | ||
| { | ||
| _owners.AddRange(owners); | ||
| foreach (var pair in objects) | ||
| foreach (var owner in pair.Value) | ||
| RegisterObject(owner, pair.Key); | ||
| } | ||
| } | ||
|
|
||
| internal ValueTask<List<Exception>?> ReleaseAsync(object instance, List<Exception>? exceptions = null) | ||
| { | ||
| ScopeOwner[] owners; | ||
| lock (_lock) | ||
| owners = _objects.TryGetValue(instance, out var registered) ? registered.ToArray() : []; | ||
| return DisposeAsync(owners, exceptions); | ||
| } | ||
|
|
||
| internal ValueTask<List<Exception>?> CompleteAsync(List<Exception>? exceptions = null) | ||
| { | ||
| ScopeOwner[] owners; | ||
| lock (_lock) | ||
| owners = _owners.ToArray(); | ||
| return DisposeAsync(owners, exceptions); | ||
| } | ||
|
|
||
| private static async ValueTask<List<Exception>?> DisposeAsync(ScopeOwner[] owners, List<Exception>? exceptions) | ||
| { | ||
| foreach (var owner in owners) | ||
| { | ||
| try | ||
| { | ||
| await owner.DisposeAsync().ConfigureAwait(false); | ||
| } | ||
| catch (Exception exception) | ||
| { | ||
| (exceptions ??= []).Add(exception); | ||
| } | ||
| } | ||
|
|
||
| return exceptions; | ||
| } | ||
|
|
||
| /// <summary>Shared by cloned rows; scope disposal is attempted once, including when it throws.</summary> | ||
| internal sealed class ScopeOwner(object? scope) | ||
| { | ||
| private int _disposed; | ||
|
|
||
| internal async ValueTask DisposeAsync() | ||
| { | ||
| if (Interlocked.Exchange(ref _disposed, 1) != 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (scope is IAsyncDisposable asyncDisposable) | ||
| { | ||
| await asyncDisposable.DisposeAsync().ConfigureAwait(false); | ||
| } | ||
| else if (scope is IDisposable disposable) | ||
| { | ||
| disposable.Dispose(); | ||
| } | ||
| } | ||
| } | ||
| } |
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 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 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 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.