runtime: [9.0.100-preview.7.24375.12] Markdig nuget package thrown "The parser is in an invalid infinite loop while trying to parse inlines for block..." exception when parsering markdown string to html
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
When running 3rd part application with the latest .NET 9 build, BlazorStrap and Blog apps failed load page content during switching pages in applications. And the command prompt window throws "The parser is in an invalid infinite loop while trying to parse inlines for block [ParagraphBlock] at position ($0, 0, 0-107" exception.
Application Name: BlazorStrap, Blog, OpenBullet2 OS: Windows 10 21H2 CPU: X64 .NET Build Number: dotnet-sdk-9.0.100-preview.7.24375.12 App or App Source checking at: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2174456
Github Link: https://github.com/chanan/BlazorStrap https://github.com/sagebind/blog
Verify Scenarios: 1). Windows 10 21H2 AMD64 + dotnet-sdk-8.0.303: Pass 2). Windows 10 21H2 AMD64 + dotnet-sdk-9.0.100-preview.7.24371.4: Pass 3). Windows 10 21H2 AMD64 + dotnet-sdk-9.0.100-preview.7.24375.12: Fail
Expected Behavior
Switch back from “Shared Parameters” to “Contribute” successfully.
Steps To Reproduce
The machine has dotnet-sdk-9.0.100-preview.7.24375.12 installed.
- Copy BlazorStrap app from the location mentioned in the devdiv bug.
- Change the “BlazorStrap.Server.runtimeconfig.json” file to let the app run against with dotnet-sdk-dotnet-sdk-9.0.100-preview.7.24375.12. “frameworks”: [ { “name”: “Microsoft.NETCore.App”, “version”: “9.0.0-preview.7.24374.12” }, { “name”: “Microsoft.AspNetCore.App”, “version”: “9.0.0-preview.7.24373.9” } ],
- Launch BlazorStrap.Server.exe.
- Access http://localhost:5000/ from browser.
- Click “Contribute” menu.
- Click “Shared Parameters” menu.
- Click “Contribute” menu.
Exceptions (if any)
Actual Result:
Switch back from “Shared Parameters” to “Contribute” failed.
Stack Trace from Command Prompt:
Unhandled exception rendering component: The parser is in an invalid infinite loop while trying to parse inlines for block [HeadingBlock] at position ($0, 0, 0-19
System.InvalidOperationException: The parser is in an invalid infinite loop while trying to parse inlines for block [HeadingBlock] at position ($0, 0, 0-19
at Markdig.Helpers.ThrowHelper.InvalidOperationException(String message)
at Markdig.Parsers.InlineProcessor.ProcessInlineLeaf(LeafBlock leafBlock)
at Markdig.Parsers.MarkdownParser.ProcessInlines(InlineProcessor inlineProcessor, MarkdownDocument document)
at Markdig.Parsers.MarkdownParser.Parse(String text, MarkdownPipeline pipeline, MarkdownParserContext context)
at Markdig.Markdown.ToHtml(String markdown, MarkdownPipeline pipeline, MarkdownParserContext context)
at BlazorStrap_Docs.Helper.MarkdownToComponent.BuildRenderTree(RenderTreeBuilder builder) in C:\Users\v-zhiyang\Desktop\YOURSELF\BlazorStrap\src\BlazorStrap-Docs\Helper\MarkdownToComponent.cs:line 93
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
Unhandled exception in circuit 'cwy0vMhNv_dIuMXmt46dyA7Bj5ivmQY-xXaGYDTlK6s'.
System.InvalidOperationException: The parser is in an invalid infinite loop while trying to parse inlines for block [HeadingBlock] at position ($0, 0, 0-19
at Markdig.Helpers.ThrowHelper.InvalidOperationException(String message)
at Markdig.Parsers.InlineProcessor.ProcessInlineLeaf(LeafBlock leafBlock)
at Markdig.Parsers.MarkdownParser.ProcessInlines(InlineProcessor inlineProcessor, MarkdownDocument document)
at Markdig.Parsers.MarkdownParser.Parse(String text, MarkdownPipeline pipeline, MarkdownParserContext context)
at Markdig.Markdown.ToHtml(String markdown, MarkdownPipeline pipeline, MarkdownParserContext context)
at BlazorStrap_Docs.Helper.MarkdownToComponent.BuildRenderTree(RenderTreeBuilder builder) in C:\Users\v-zhiyang\Desktop\YOURSELF\BlazorStrap\src\BlazorStrap-Docs\Helper\MarkdownToComponent.cs:line 93
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
.NET Version
9.0.100-preview.7.24375.12
Anything else?
Minimal Repro steps (repro demo attached MarkdownToHtmlDemo.zip): 1.Create a Blazor Server App project. 2. Install Markdig nuget package. 3. Add a MarkdownToComponent.cs class file with following content:
public class MarkdownToComponent : ComponentBase
{
[Parameter] public RenderFragment? ChildContent { get; set; }
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
string s = @"The following are **guide lines** on howto contribute. You input is always **welcomed** and **appreciated**.
### Feature Request
---
If you feel BlazorStrap is lacking a feature you need or just want please create a issue with the following.
* What does it do
* Why you feel it should exist
* How can it help others";
string ms = Markdown.ToHtml(s);
}
}
- Add above component to Counter.razor page.
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
<MarkdownToComponent>Test</MarkdownToComponent>
- Build the project.
- Update the runtime.config file to run against with dotnet-sdk-dotnet-sdk-9.0.100-preview.7.24375.12.
"frameworks": [
{
"name": "Microsoft.NETCore.App",
"version": "9.0.0-preview.7.24374.12"
},
{
"name": "Microsoft.AspNetCore.App",
"version": "9.0.0-preview.7.24373.9"
}
],
- Launch the project.
- Access http://localhost:5000/ from browser.
- Navigate to Counter page.
- Refresh the Counter page 7 times.
Expected Result: The Counter page refresh 7 time successfully.
Actual Result: The Counter page refresh failed at the 7th refresh with following error:
System.InvalidOperationException: 'The parser is in an invalid infinite loop while trying to parse inlines for block [ParagraphBlock] at position ($0, 0, 0-107'
Stack Trace:
at Markdig.Helpers.ThrowHelper.InvalidOperationException(String message)
at Markdig.Parsers.InlineProcessor.ProcessInlineLeaf(LeafBlock leafBlock)
at Markdig.Parsers.MarkdownParser.ProcessInlines(InlineProcessor inlineProcessor, MarkdownDocument document)
at Markdig.Parsers.MarkdownParser.Parse(String text, MarkdownPipeline pipeline, MarkdownParserContext context)
at Markdig.Markdown.ToHtml(String markdown, MarkdownPipeline pipeline, MarkdownParserContext context)
at MarkdownToHtmlDemo.MarkdownToComponent.BuildRenderTree(RenderTreeBuilder builder) in E:\Demos\MarkdownToHtmlDemo\MarkdownToHtmlDemo\MarkdownToComponent.cs:line 26
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
Findings:
- 2 apps are all referenced Markdig nuget package and the exception in 2 apps are both thrown from Markdown.ToHtml() method which is used to convert a markdown file sting to html.
- We tried to reference the Markdig (version 0.37.0) source code instead of install it from nuget package manager, it doesn’t repro in debug mode. But if we enable compiler optimizations on debug mode for Markdig project, this issue will repro again. It doesn’t have this issue on build#dotnet-sdk-9.0.100-preview.7.24371.4 . We think something changed with optimization code in 9.0.100-preview.7.24375.12 which cause this issue.
- The Markdig nuget package is used by 5.8k users. It may affect more users in real world.
- This issue will occurs after switching pages in applications multiple times or refresh the page multiple times. The first time load the page can successful.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (15 by maintainers)
Commits related to this issue
- JIT: limit phi-refinement to loop-invariant VNs PhiArg VNs should be invariant in the loop that contains the Phi, otherwise the same VN may refer to values from more than one iteration. Fixes #10579... — committed to AndyAyersMS/runtime by AndyAyersMS 2 years ago
- JIT: limit phi-refinement to loop-invariant VNs (#106229) PhiArg VNs should be invariant in the loop that contains the Phi, otherwise the same VN may refer to values from more than one iteration. ... — committed to shushanhf/runtime by AndyAyersMS 2 years ago
- JIT: limit phi-refinement to loop-invariant VNs (#106229) PhiArg VNs should be invariant in the loop that contains the Phi, otherwise the same VN may refer to values from more than one iteration. ... — committed to mikelle-rogers/runtime by AndyAyersMS 2 years ago
I was able to pinpoint to https://github.com/dotnet/runtime/pull/104752 as a source of regression (f455188a11c).
This was done by doing semi-manual (rejecting obviously not culprits and repro requires refreshing the page) binary search between commits 1f70f0cc663b5d45f77840e9728386e5fa7944f9 and eedf30ebac80ca81b88c4c452614522a5ece1b27. I had to do it twice because first time I was getting AVs caused by https://github.com/dotnet/runtime/pull/104336 and then had to do it second time with revert of that commit - the AV is already reverted by https://github.com/dotnet/runtime/pull/105234 though.
I tried to run original repro after reverting https://github.com/dotnet/runtime/pull/104752 but I hit non-obvious merge conflict and it’s already end of the day for me so will leave it here.
I didn’t have much luck narrowing down the repro (other than removing Counter page and putting MarkDownToComponent directly in the Index but that also causes you need to refresh a bit more than 7 times) - it seems to be somewhere between Razor and Markdig - I’ve tried isolating Markdig piece (i.e. stressed this under multiple threads) but without luck and I’m not familiar with Razor enough to simplify this further quickly.
It would need investigation. @jeffhandley could y’all take a look? It looks like a 3rd party nuget break, but perhaps we had a subtle change causing the break.
@Junjun-zhao thanks for the additional details.
This seems to be a regression somewhere in the runtime. I’m transferring the issue