Skip to content

Unobserved exceptions on broken @import declarations #194

@prostakov

Description

@prostakov

Prerequisites

  • Can you reproduce the problem in a MWE?
  • Are you running the latest version of AngleSharp.Css?
  • 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.Xml for Xml support)
  • Did you perform a search in the issues?

Description

I am able to reproduce unobserved task exceptions with empty @import declarations.

Steps to Reproduce

The main test looks like this:

[Fact]
public void Test()
{
    var cssParsingOptions = new CssParserOptions
    {
        IsIncludingUnknownDeclarations = true,
        IsIncludingUnknownRules = true,
        IsToleratingInvalidSelectors = true
    };
    var config = Configuration.Default.WithCss(cssParsingOptions);
    var browsingContext = BrowsingContext.New(config);
    var htmlParser = new HtmlParser(new HtmlParserOptions(), browsingContext);

    var input =
        """
            <!DOCTYPE HTML>
            <html>
                <head>
                </head>
                <body>
                    <style type="text/css">
                        @import ;
                    </style>
                </body>
            </html>
        """;

    // Act
    var (r, ex) = AssertExtensions.ThrowsUnobservedTaskException(() => htmlParser.ParseDocument(input));

    // Assert
    ex!.InnerException.Should().NotBeNull();
    ex.InnerException!.Message.Should().StartWith("Object reference not set to an instance of an object.");
}

With additional helper to catch unobserved exceptions:

public static class AssertExtensions
{
    public static (T result, Exception? unobservedException) ThrowsUnobservedTaskException<T>(Func<T> action)
    {
        Exception? unobservedException = null;
        var unobservedExceptionHandler = new EventHandler<UnobservedTaskExceptionEventArgs>((sender, e) =>
        {
            unobservedException = e.Exception;
            e.SetObserved();
        });

        TaskScheduler.UnobservedTaskException += unobservedExceptionHandler;

       try
       {
            var result = action();

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            Task.Delay(1000).Wait();

            return (result, unobservedException);
        }
       finally
       {
           TaskScheduler.UnobservedTaskException -= unobservedExceptionHandler;
       }
    }
}

Expected Behavior

Expect not to throw unobserved exceptions.

Actual Behavior

Throws null-ref on this line:
https://github.com/AngleSharp/AngleSharp.Css/blob/devel/src/AngleSharp.Css/Parser/CssParser.cs#L243

Possible Solution / Known Workarounds

Add more proper null-checks in place where null-ref is thrown?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions