Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronous operations are disallowed error with AspNetCore integration when using HttpResponseDataExtensions.WriteString #2184

Closed
kshyju opened this issue Jan 8, 2024 · 4 comments
Assignees
Labels
area: http Items related to experience improvements for HTTP triggers

Comments

@kshyju
Copy link
Member

kshyju commented Jan 8, 2024

Repro steps:
  1. Create a new net8 isolated app with one http trigger.

  2. Add a reference to the Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore NuGet package, version 1.0.0 or later to your project.

  3. Update startup code

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .Build();

host.Run();

Update the HttpTrigger function class to have below 3 functions

[Function("Foo")]
public HttpResponseData Foo([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
    var response = req.CreateResponse(HttpStatusCode.OK);
    response.WriteString("Welcome to Azure Functions!");

    return response;
}

[Function("Foo2")]
public async Task<HttpResponseData> Foo2([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
    var response = req.CreateResponse(HttpStatusCode.OK);
    await response.WriteStringAsync("Welcome to Azure Functions!");

    return response;
}

[Function("Bar")]
public IActionResult Bar([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req)
{
    return new OkObjectResult($"Welcome to Azure Functions, {req.Query["name"]}!");
}

Run the app and execute all functions. "Bar" and "Foo2" will work. "Foo" will fail with below exception.

System.InvalidOperationException
HResult=0x80131509
Message=Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead.
Source=Microsoft.AspNetCore.Server.Kestrel.Core
StackTrace:
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at Microsoft.Azure.Functions.Worker.Http.HttpResponseDataExtensions.WriteString(HttpResponseData response, String value, Encoding encoding)
at FunctionApp207.Function1.Foo(HttpRequestData req) in C:\Dev\Temp\FunctionApp207\Function1.cs:line 17

Packages used

  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
  </ItemGroup>
@satvu satvu added area: http Items related to experience improvements for HTTP triggers and removed Needs: Triage (Functions) labels Jan 8, 2024
@jviau
Copy link
Contributor

jviau commented Jan 9, 2024

Synchronous IO is disallowed by default in kestrel:

https://stackoverflow.com/questions/47735133/asp-net-core-synchronous-operations-are-disallowed-call-writeasync-or-set-all

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

Do we want to set that to true by default? Or leave that to customers to decide?

@fabiocav
Copy link
Member

After discussion, here are some of the ideas on what we should do instead of enabling synchronous IO:

  • Update all templates to make sure we're using async patterns when working with HttpRequestData
  • Create an analyzer that will provide compile time validation and code suggestion to fix/prevent the issue
  • Add this information to the migration guides (including highlighting the fact that ASP.NET Core behaviors apply when using the integration)

@fabiocav
Copy link
Member

Assigning to the next spring to decompose this into different issues we can track.

@kshyju
Copy link
Member Author

kshyju commented Feb 14, 2024

Opened #2286 and Azure/azure-functions-templates#1509

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: http Items related to experience improvements for HTTP triggers
Projects
None yet
Development

No branches or pull requests

4 participants