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

Transfer closed with outstanding read data remaining #2893

Open
aaramians opened this issue Dec 10, 2024 · 0 comments
Open

Transfer closed with outstanding read data remaining #2893

aaramians opened this issue Dec 10, 2024 · 0 comments
Labels
Needs: Triage (Functions) potential-bug Items opened using the bug report template, not yet triaged and confirmed as a bug

Comments

@aaramians
Copy link

Description

When an exception is thrown, if there is a custom middleware that intercepts, and responds with a payload, transfer doesn't complete.

if I have to guess, because the length of response is not set, chunked transfer encoding is selected, however end mark is not properly sent when an exception is raised.

The reason I'm raising an exception is to let function invocation know that this function failed, I couldn't find a way to let the invocation know it failed (error) without raising an exception (I believe it would be nice to have...)

Steps to reproduce

Example

using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Functions.Worker.Middleware;
using Azure;
using Microsoft.Azure.Functions.Worker.Http;
using System.Text.Json;

namespace FunctionApp1
{
    public class Function1
    {
        public Function1()
        {
        }

        [Function("Run")]
        public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
        {
            throw new InvalidOperationException();
        }
    }

}

internal class Program
{
    public class ExceptionHandlingMiddleware : IFunctionsWorkerMiddleware
    {
        public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
        {
            try
            {
                await next(context);
            }
            catch (Exception ex)
            {
                var httpRequestData = await context.GetHttpRequestDataAsync();

                var httpResponseData = httpRequestData.CreateResponse();

                httpResponseData.Headers.Add("Content-Type", "application/json");

                var payload = JsonSerializer.Serialize(new { error = "something went wrong" });

                httpResponseData.StatusCode = System.Net.HttpStatusCode.InternalServerError;

                await httpResponseData.WriteStringAsync(payload);

                throw;
            }
        }
    }

    private static void Main(string[] args)
    {
        var builder = FunctionsApplication.CreateBuilder(args);
        builder.UseMiddleware<ExceptionHandlingMiddleware>();
        builder.ConfigureFunctionsWebApplication();
        builder.Build().Run();
    }
}

Output:

curl http://localhost:7063/api/Run -v
* Host localhost:7063 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:7063...
*   Trying 127.0.0.1:7063...
* Connected to localhost (127.0.0.1) port 7063
> GET /api/Run HTTP/1.1
> Host: localhost:7063
> User-Agent: curl/8.9.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 500 Internal Server Error
< Content-Type: application/json
< Date: Tue, 10 Dec 2024 22:30:15 GMT
< Server: Kestrel
< Transfer-Encoding: chunked
<
{"error":"something went wrong"}* transfer closed with outstanding read data remaining
* closing connection #0
curl: (18) transfer closed with outstanding read data remaining
@aaramians aaramians added the potential-bug Items opened using the bug report template, not yet triaged and confirmed as a bug label Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Triage (Functions) potential-bug Items opened using the bug report template, not yet triaged and confirmed as a bug
Projects
None yet
Development

No branches or pull requests

1 participant