Skip to content

Make CreatorCache thread-safe #109

Description

@badnickname

Bug Report

Prerequisites

  • Can you reproduce the problem in a MWE?
  • Are you running the latest version of AngleSharp?
  • Did you check the FAQs to see if that helps you?
  • Are you reporting to the correct repository? (there are multiple AngleSharp libraries, e.g., AngleSharp.Css for CSS support)
  • Did you perform a search in the issues?

For more information, see the CONTRIBUTING guide.

Description

Running OpenAsync concurrently leads to exception because of thread-unsafe collections inside CreatorCache

Steps to Reproduce

[Test]
public void CustomEventConstructedConcurrently()
{
    var ctx1 = BrowsingContext.New(Configuration.Default.WithJs());
    var ctx2 = BrowsingContext.New(Configuration.Default.WithJs());
    var html = "<!doctype html><div id=result></div><script>var ev = new CustomEvent('foo'); document.querySelector('#result').textContent = ev.type;</script>";
    Parallel.Invoke(() => Assert(ctx1), () => Assert(ctx2));
    return;

    void Assert(IBrowsingContext context) => Task
        .Run(async () =>
        {
            var document = await context.OpenAsync(m => m.Content(html));
            var result = document.QuerySelector("#result").TextContent;
            NUnit.Framework.Assert.AreEqual("foo", result);
        })
        .Wait();
}

Possible Solution

Replace each Dictionary in CreatorCache with a ConcurrentDictionary

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions