Azure / Azure/azure-functions-dotnet-worker
When using ASP.NET Core integration, ObjectDisposedException is thrown when the request is cancelled
- Dominant language
- C#
- Stars
- 466
- Forks
- 215
- Avg merge
- 3d 10h
- Merged PRs (30d)
- 7
Description
# Summary
When using ASP.NET Core integration, if a request thrown to an HttpTrigger developed is cancelled by the client side, an `ObjectDisposedException` occurs. The stack trace is as follows. This does not occur when ASP.NET Core integration is not used.
## Stack trace
```
System.Private.CoreLib: Exception while executing function: Functions.Http_Sample. System.Private.CoreLib: Result: Failure
Exception: System.ObjectDisposedException: Request has finished and HttpContext disposed.
Object name: 'HttpContext'.
at Microsoft.AspNetCore.Http.DefaultHttpContext.ThrowContextDisposed()
at Microsoft.AspNetCore.Http.DefaultHttpContext.get_Features()
at Microsoft.AspNetCore.Routing.RoutingHttpContextExtensions.GetRouteData(HttpContext httpContext)
at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.FunctionsHttpProxyingMiddleware.Invoke(FunctionContext context, FunctionExecutionDelegate next) in D:\a\_work\1\s\extensions\Worker.Extensions.Http.AspNetCore\src\FunctionsMiddleware\FunctionsHttpProxyingMiddleware.cs:line 52
at Microsoft.Azure.Functions.Worker.FunctionsApplication.InvokeFunctionAsync(FunctionContext context) in D:\a\_work\1\s\src\DotNetWorker.Core\FunctionsApplication.cs:line 77
at Microsoft.Azure.Functions.Worker.Handlers.InvocationHandler.InvokeAsync(InvocationRequest request) in D:\a\_work\1\s\src\DotNetWorker.Grpc\Handlers\InvocationHandler.cs:line 88
Stack: at Microsoft.AspNetCore.Http.DefaultHttpContext.ThrowContextDisposed()
at Microsoft.AspNetCore.Http.DefaultHttpContext.get_Features()
at Microsoft.AspNetCore.Routing.RoutingHttpContextExtensions.GetRouteData(HttpContext httpContext)
at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.FunctionsHttpProxyingMiddleware.Invoke(FunctionContext context, FunctionExecutionDelegate next) in D:\a\_work\1\s\extensions\Worker.Extensions.Http.AspNetCore\src\FunctionsMiddleware\FunctionsHttpProxyingMiddleware.cs:line 52
at Microsoft.Azure.Functions.Worker.FunctionsApplication.InvokeFunctionAsync(FunctionContext context) in D:\a\_work\1\s\src\DotNetWorker.Core\FunctionsApplication.cs:line 77
at Microsoft.Azure.Functions.Worker.Handlers.InvocationHandler.InvokeAsync(InvocationRequest request) in D:\a\_work\1\s\src\DotNetWorker.Grpc\Handlers\InvocationHandler.cs:line 88.
```
## Applicable section
- https://github.com/Azure/azure-functions-dotnet-worker/blob/40d73ee8107eb48c63bcaef868522fcbcaf0d549/extensions/Worker.Extensions.Http.AspNetCore/src/FunctionsMiddleware/FunctionsHttpProxyingMiddleware.cs#L52C100-L52C100
# Excpected behavior
- Terminate the request appropriately without causing an `ObjectDisposedException`.
# Reproduction code
- [HttpContextDisposedSample.zip](https://github.com/Azure/azure-functions-dotnet-worker/files/12721060/HttpContextDisposedSample.zip)
```cs
using Microsoft.Extensions.Hosting;
var host
= new HostBuilder()
.ConfigureFunctionsWebApplication()
.Build();
host.Run();
```
```cs
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace UsingAspNetCoreIntegration;
public class Http_Sample
{
private readonly ILogger _logger;
public Http_Sample(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger();
}
[Function(nameof(Http_Sample))]
public async Task EntryPoint(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "sample")] HttpRequest request,
FunctionContext context)
{
// note:
// - ObjectDisposedException is thrown when the request is cancelled.
try
{
var delay = TimeSpan.FromSeconds(10);
await Task.Delay(delay, context.CancellationToken);
return new OkResult();
}
catch (Exception ex)
{
this._logger.LogWarning(ex, "Error occurred");
return new StatusCodeResult(500);
}
}
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.