azure-functions-dotnet-worker: When using ASP.NET Core integration, ObjectDisposedException is thrown when the request is cancelled
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
Excpected behavior
- Terminate the request appropriately without causing an
ObjectDisposedException.
Reproduction code
using Microsoft.Extensions.Hosting;
var host
= new HostBuilder()
.ConfigureFunctionsWebApplication()
.Build();
host.Run();
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<Http_Sample>();
}
[Function(nameof(Http_Sample))]
public async Task<IActionResult> 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);
}
}
}
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 2
- Comments: 15 (5 by maintainers)
My company is also running into this same issue, it started occurring after moving our azure function over to dotnet isolated on 9/13
@satvu We updated
Microsoft.Azure.Functions.Workerto v1.23.0 on 2024/08/16 and continued our service operations. However, it appears that the issue recurred once on 2024/08/22. We hope this information can serve as useful feedback.Update: the investigation for this issue has revealed a larger problem that would require a design review and subsequent refactoring. We will skip the temporary mitigation as it only prevents some scenarios of this error message and introduces new behaviors that differ from the desired end result of a long-term fix.
To follow progress on long-term fix items, you can follow #2180 and #2189.
@im-samz - Wanted to tag you on this one. About to open a case here for a function in WestUS.
We’ve made a lot of changes in this area in the past few months, including fixes in both the host and the extension package related to cancellation flow and error handling. I am unable to repro this error again with the original repro code provided (despite being able to previously) with the latest host version and extension package.
Will let the bot close this unless there are any additional reports/complaints with the latest bits.
@satvu Based on my experience using ASP.NET Core Integration in a production environment, it is only “occasionally detected”. While it is not catastrophic, I do recognize that it is an exception that should not normally occur (= should not be detected).