Skip to content

Synchronous call inside XDocument.LoadAsync #100239

@Yozer

Description

@Yozer

Description

Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.

at MyMiddleware.InvokeAsync(HttpContext context) 
at System.Xml.Linq.XDocument.LoadAsync(TextReader textReader, LoadOptions options, CancellationToken cancellationToken)
at System.Xml.Linq.XDocument.LoadAsyncInternal(XmlReader reader, LoadOptions options, CancellationToken cancellationToken)
at System.Xml.Linq.XDeclaration..ctor(XmlReader r)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.ParseElement()
at System.Xml.XmlTextReaderImpl.ParseAttributes()
at System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, Char quoteChar, NodeData attr)
at System.Xml.XmlTextReaderImpl.ReadData()
at System.IO.StreamReader.Read(Char[] buffer, Int32 index, Int32 count)
at System.IO.StreamReader.ReadSpan(Span`1 buffer)
at System.IO.StreamReader.ReadBuffer(Span`1 userBuffer, Boolean& readToUserBuffer)
at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.Read(Span`1 buffer)
at System.IO.Stream.Read(Span`1 buffer)
at System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.

Reproduction Steps

I don't have the actual XML request that caused it but I think it's pretty clear from call stack that LoadAsyncInternal creates XDeclaration and the ctor there reads attributes and does it in a non-async way.

Expected behavior

XDocument.LoadAsync should be fully async

Actual behavior

Throws Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.

Regression?

No

Known Workarounds

Allow syncIO

builder.WebHost.ConfigureKestrel((_, options) =>
{
    options.AllowSynchronousIO = true;
});

Configuration

latest aspnet core docker image: 8.0.3-bookworm-slim-amd64
https://github.com/dotnet/dotnet-docker/blob/9ffd75ed9a9a8790e7c7af5c925eefe7e4015bc1/src/aspnet/8.0/bookworm-slim/amd64/Dockerfile

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions