graphql-dotnet: System.InvalidOperationException: Synchronous operations are disallowed.

Description

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

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.StreamReader.ReadBuffer(Span`1 userBuffer, Boolean& readToUserBuffer)
   at System.IO.StreamReader.ReadSpan(Span`1 buffer)
   at System.IO.StreamReader.Read(Char[] buffer, Int32 index, Int32 count)
   at Newtonsoft.Json.JsonTextReader.ReadData(Boolean append, Int32 charsRequired)
   at Newtonsoft.Json.JsonTextReader.ParseValue()
   at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)
   at GraphQL.Server.Transports.AspNetCore.GraphQLHttpMiddleware`1.Deserialize[T](Stream s)
   at GraphQL.Server.Transports.AspNetCore.GraphQLHttpMiddleware`1.InvokeAsync(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.InvokeCore(HttpContext context, ICorsPolicyProvider corsPolicyProvider)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Steps to reproduce

Upgrade to .NET Core 3.0 preview 3 or newer.

Expected result

Synchronous IO will be made asynchronous.

Actual result

Synchronous IO is still synchronous.

Workaround

Put this in the ConfigureServices section of Startup.cs.

// kestrel
services.Configure<KestrelServerOptions>(options =>
{
    options.AllowSynchronousIO = true;
});

// IIS
 services.Configure<IISServerOptions>(options =>
{
    options.AllowSynchronousIO = true;
});

Environment

.NET Core SDK (reflecting any global.json):
 Version:   3.0.100-preview4-011223
 Commit:    118dd862c8

Host (useful for support):
  Version: 3.0.0-preview4-27615-11
  Commit:  ee54d4cbd2

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 7
  • Comments: 24 (13 by maintainers)

Most upvoted comments

Just ran into this as well in .NET Core 3.0 Preview 7.

BTW the workaround above is Kestrel-specific. If you’re running on IIS, this is the workaround:

            services.Configure<IISServerOptions>(options =>
            {
                options.AllowSynchronousIO = true;
            });

The IIS and Kestrel settings should be avoided if possible. Data should be asynchronously buffered before parsing or you risk threadpool starvation.

that are going against the framework rules

By the way, what framework rules? ASP.NET Core rules? These rules are not an absolute truth, they relate to a specific runtime environment and they cannot be tried to forcefully apply to absolutely everything. Although I understand the essence of your thought.

I guessed about it.

That doesn’t preclude async buffering before passing the stream to Newtonsoft.Json. That’s what MVC did.

Yes. But too much fuss.

@Jonatthu You can open a new issue about migration to System.Text.Json so it will be more focused on new features and not on aforementioned errors.

Well, we need to choose a lesser evil. When/if Newtonsoft.Json package provides a method for asynchronous deserialization then we can use it. Although honestly I believe that this will never happen. Probably Newtonsoft.Json will just be replaced in future by a serializer from System.Text.Json.

@Jonatthu There is nothing to do with it in this project. You can configure aforementioned settings for IIS or Kestrel.

@Eilon Thanks for the assist!