Azure / Azure/azure-functions-dotnet-worker

Transfer closed with outstanding read data remaining

Open
#2,893 0 comments 0 reactions 1 assignee Claimed by @jviau View on GitHub
needs-discussion needs-investigation potential-bug
Dominant language
C#
Stars
466
Forks
215
Avg merge
3d 10h
Merged PRs (30d)
7

Description

### 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
```csharp
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();
builder.ConfigureFunctionsWebApplication();
builder.Build().Run();
}
}
```

Output:
```powershell
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
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.