KEMBAR78
Synchronous calls in XsdValidatingReader · Issue #78802 · dotnet/runtime · GitHub
Skip to content

Synchronous calls in XsdValidatingReader #78802

@CommonGuy

Description

@CommonGuy

Description

When using an XmlReader with schema validation enabled in an async context, the underlying XsdValidatingReader still performs synchronous calls. This can lead to an exception when used on a HTTP request body in Kestrel, as it disallows sync IO access by default.

This is similar to #31737 and a bunch of other issues related to sync calls while doing XML (de-)serialization.

Reproduction Steps

Rough example:

var settings = new XmlReaderSettings();
settings.Async = true;
settings.ValidationType = ValidationType.Schema;
settings.Schemas = mySchemaSet;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += (ValidationEventHandler) ((_, args) =>
{
    if (args.Severity == XmlSeverityType.Error)
        throw args.Exception;
});

var reader = XmlReader.Create(stream, settings);

while (...)
{
    await XNode.ReadFromAsync(reader, CancellationToken.None);
}

Stack trace:

   at System.Xml.XmlTextReaderImpl.ReadData()
   at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
   at System.Xml.XmlTextReaderImpl.FinishPartialValue()
   at System.Xml.XmlTextReaderImpl.get_Value()
   at System.Xml.XsdValidatingReader.GetStringValue()
   at System.Xml.Schema.XmlSchemaValidator.ValidateText(String elementStringValue, XmlValueGetter elementValueGetter)
   at System.Xml.Schema.XmlSchemaValidator.ValidateText(XmlValueGetter elementValue)
   at System.Xml.XsdValidatingReader.ProcessReaderEventAsync()
   at System.Xml.XsdValidatingReader.ReadAsync_Read(Task`1 task)
   at System.Xml.XsdValidatingReader.ReadAsync()
   at System.Xml.XmlAsyncCheckReader.ReadAsync()
   at System.Xml.Linq.XContainer.ReadContentFromAsync(XmlReader r, CancellationToken cancellationToken)
   at System.Xml.Linq.XContainer.ReadContentFromAsync(XmlReader r, LoadOptions o, CancellationToken cancellationToken)
   at System.Xml.Linq.XElement.ReadElementFromAsync(XmlReader r, LoadOptions o, CancellationToken cancellationTokentoken)
   at System.Xml.Linq.XElement.CreateAsync(XmlReader r, CancellationToken cancellationToken)
   at System.Xml.Linq.XNode.ReadFromAsyncInternal(XmlReader reader, CancellationToken cancellationToken)

Expected behavior

No synchronous calls (=no synchronous IO access) should be performed.

Actual behavior

Synchronous calls are being performed.

Regression?

No response

Known Workarounds

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions