-
-
Notifications
You must be signed in to change notification settings - Fork 122
fix: guard BunitHtmlParser.documents against concurrent access #1893
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ internal sealed class BunitHtmlParser : IDisposable | |
| private readonly IBrowsingContext context; | ||
| private readonly HtmlParser htmlParser; | ||
| private readonly List<IDocument> documents = new(); | ||
| private readonly object documentsLock = new(); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="BunitHtmlParser"/> class | ||
|
|
@@ -151,15 +152,34 @@ private static (IElement? Context, string? MatchedElement) GetParseContextFromTa | |
| private async Task<IDocument> GetNewDocumentAsync() | ||
| { | ||
| var result = await context.OpenNewAsync().ConfigureAwait(false); | ||
| documents.Add(result); | ||
|
|
||
| lock (documentsLock) | ||
| { | ||
| documents.Add(result); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void Dispose() | ||
| { | ||
| context.Dispose(); | ||
| foreach (var doc in documents) | ||
|
|
||
| // Parse() can be running on another thread while this executes, e.g. a | ||
| // WaitForAssertion/WaitForState condition being evaluated on the | ||
| // renderer's dispatcher while the test's BunitContext is being | ||
| // disposed. Take a snapshot under the lock instead of enumerating the | ||
| // live list, which would otherwise throw "Collection was modified" if a | ||
| // parse completed mid-loop. | ||
| IDocument[] documentsToDispose; | ||
| lock (documentsLock) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The lock can go through the while
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also |
||
| { | ||
| documentsToDispose = documents.ToArray(); | ||
| documents.Clear(); | ||
| } | ||
|
|
||
| foreach (var doc in documentsToDispose) | ||
| { | ||
| doc.Dispose(); | ||
| } | ||
|
|
||
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.
Technically the lock here is not needed - the only usage does a synchronous wait for it.