Azure / Azure/azure-functions-host

[FromServices] race condition leads to null

Open
#5,161 3 comments 0 reactions 1 assignee Claimed by @fabiocav View on GitHub
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

There are several places in our code where we use `[FromServices]` to resolve a host level service. In certain situations, even if the action is marked [RequiresRunningHost] a null can be returned. In a recent pr [here](https://github.com/Azure/azure-functions-host/commit/33bf8bca788cb2fe58a5e00c444f21616082aa20) we worked around this for a particular API, with the follow up being to put in a general solution for this so we're doing this consistently across all actions.

E.g. I'm seeing exceptions in production now for the new runtime scale endpoint (HostController.GetScaleStatus) due to this issue.

```
System.InvalidOperationException : No service for type 'Microsoft.Azure.WebJobs.Script.IScriptJobHost' has been registered.
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider,Type serviceType)
at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ServicesModelBinder.BindModelAsync(ModelBindingContext bindingContext)
at async Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(??)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.g__Bind|0(??)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next,Scope& scope,Object& state,Boolean& isCompleted)
at async Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.Azure.WebJobs.Script.WebHost.Middleware.FunctionInvocationMiddleware.Invoke(HttpContext context) at C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\Middleware\FunctionInvocationMiddleware.cs : 52
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.Azure.WebJobs.Script.Middleware.DefaultMiddlewarePipeline.<>c.b__7_0(??) at C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\Middleware\JobHostMiddleware\DefaultMiddlewarePipeline.cs : 31
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.Azure.WebJobs.Script.WebHost.Middleware.JobHostPipelineMiddleware.Invoke(HttpContext httpContext,IJobHostMiddlewarePipeline middleware) at C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\Middleware\JobHostPipelineMiddleware.cs : 26
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.Azure.WebJobs.Script.WebHost.Middleware.ResponseContextItemsCheckMiddleware.Invoke(HttpContext context) at C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\Middleware\ResponseContextItemsCheckMiddleware.cs : 29
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.Azure.WebJobs.Script.WebHost.Middleware.HomepageMiddleware.Invoke(HttpContext context) at C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\Middleware\HomepageMiddleware.cs : 34
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.AspNetCore.Buffering.ResponseBufferingMiddleware.Invoke(HttpContext httpContext) at C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\Middleware\Buffering\ResponseBufferingMiddleware.cs : 38
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.Azure.WebJobs.Script.WebHost.Middleware.ExceptionMiddleware.Invoke(HttpContext context) at C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\Middleware\ExceptionMiddleware.cs : 28
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.