Skip to content

Fix concurrent HtmlParser corruption by removing static singleton#956

Merged
Tyrrrz merged 3 commits into
primefrom
copilot/fix-multiple-youtubeclient-instances
May 28, 2026
Merged

Fix concurrent HtmlParser corruption by removing static singleton#956
Tyrrrz merged 3 commits into
primefrom
copilot/fix-multiple-youtubeclient-instances

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 28, 2026

HtmlParser was a static readonly singleton, making it shared across all YoutubeClient instances and threads. AngleSharp's HtmlParser is not thread-safe — concurrent calls corrupt its internal Dictionary state, throwing InvalidOperationException.

Changes

  • YoutubeExplode/Utils/Html.cs: Replace the static HtmlParser field with a new instance per Parse() call, with a comment explaining the thread-safety rationale.
// Before
private static readonly HtmlParser HtmlParser = new();
public static IHtmlDocument Parse(string source) => HtmlParser.ParseDocument(source);

// After
public static IHtmlDocument Parse(string source) => new HtmlParser().ParseDocument(source);

This allows multiple YoutubeClient instances to be used concurrently without corrupting shared parser state.

Copilot AI changed the title [WIP] Fix concurrent usage of YoutubeClient instances due to static HtmlParser Fix concurrent HtmlParser corruption by removing static singleton May 28, 2026
Copilot finished work on behalf of Tyrrrz May 28, 2026 16:12
Copilot AI requested a review from Tyrrrz May 28, 2026 16:12
@Tyrrrz Tyrrrz added the bug label May 28, 2026
@Tyrrrz Tyrrrz marked this pull request as ready for review May 28, 2026 16:15
Copilot AI review requested due to automatic review settings May 28, 2026 16:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes thread-safety corruption in Html.Parse by replacing the shared static HtmlParser singleton with a per-call instance, since AngleSharp's HtmlParser is not thread-safe.

Changes:

  • Remove static HtmlParser field in Html utility class.
  • Instantiate a new HtmlParser per Parse() call with explanatory comment referencing issue #955.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Tyrrrz Tyrrrz merged commit 228426f into prime May 28, 2026
8 checks passed
@Tyrrrz Tyrrrz deleted the copilot/fix-multiple-youtubeclient-instances branch May 28, 2026 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't use multiple YoutubeClient instances concurrently because of static HtmlParser

3 participants