Open Markeli opened 1 year ago
Where in your startup code do you actually register those services? Your issue description doesn't include any parts of your configuration for IServiceCollection
that registers ISftpClientFactory
or ITempFileStreamFactory
.
Hi @Markeli. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
@martincostello, updated issue description with services registration example.
We don't decorate or override this services after registration. They are definitely registered. They are injected into controller's action in 99.99% of requests.
@Markeli I've been trying to see if I can reproduce this locally with a minimal repo containing a web app configured with two singleton services in the DI container and a controller consuming them via the attributes and 1000 requests executed sequentially but I couldn't repro this. Some questions:
If you're able to get a minimal repro going, that would be helpful.
This also affect in our web API.
Note that it's only affect 1-2% of 10K RPS, which means it's still a lot if we see in our monitor.
Our workaround is to use HttpContext and get service from there
@yunusefendi52 Thanks for sharing this info! Does this always happen in the same controller?
If you're able to share any redacted logs from the repros that would be helpful.
@captainsafia Yes this always happen in the same controller with [FromServices]
I don't have the logs anymore; it's months ago and our logs retention is only around 2 months
@captainsafia, unfortunately I can't provide a minimal repo. We've tried to create a minimal repo, make a workload, but it works fine. But in a production bug is reproduced sometimes.
There are answers for your questions:
ILoggerFactory
. We use NLog as out log provider. Here is out nuget packages related to logging:
@yunusefendi52, can you please share info about your app? Do you use NLog? And what dotnet version you use to host your app?
@captainsafia, what kind of logs can help you to understand the reason? We can't share all our logs, but we can save for you useful logs from Microsoft classes or something else.
@Markeli we use Serilog and dotnet 6
We have found steps to reproduce bug.
There are at least 2 case when we've gotten this error.
Dependencies are injected.
Dependencies not injected.
There are demo for this case. Bug is reproduced both at .NET 6 and .NET 8.
[Uploading ExceptionDemonstration.Net6.zip…]() ExceptionDemonstration.Net8.zip
Bug occurs when request was cancelled by user before model binding completion.
Task.Delay(TimeSpan.FromSeconds(15);
In both cases ASP.NET Core model binders and services providers handles exception/request cancellation, doesn't complete their logic and continues request's flow as usual. But due to request cancelation/exception request going to controllers in some kind of broken state.
It's possible to create custom action filter that checks request cancellation on tries to check existence of request size limit exception and throws exception that should be correctly handle by error handling middleware.
Here is out workaround sample:
public class BinderFailureActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
// chec request cancellation
context.HttpContext.RequestAborted.ThrowIfCancellationRequested();
// try to check thath all DI provided services are binded
var failedServiceParameter = context.ActionDescriptor.Parameters
.Where(x => x.BindingInfo?.BindingSource == BindingSource.Services)
.Where(x => !context.ActionArguments.ContainsKey(x.Name))
.FirstOrDefault();
// if some of service is missing, let's throw an exception
if (failedServiceParameter != null)
throw new BindingFailureException(context.ActionDescriptor.DisplayName, failedServiceParameter.Name);
}
}
Is there an existing issue for this?
Describe the bug
We have controller's action like this:
We injects
ISftpClientFactory
andITempFileStreamFactory
instances as dependencies. They marked with[FromServices]
. All services are registered.99.99% of all request processed correctly. But sometimes we get null instead of this services. And null checks throws an exception:
We doesn't change request's pipeline dynamically. Here is our configured pipeline:
All of this middleware doesn't change pipeline's flow.
Here is example of registration of those services:
Expected Behaviour
Dependencies are always not null
Steps To Reproduce
No specific steps, the error occurs randomly.
Exceptions (if any)
.NET Version
6.0.0
Anything else?
Host (useful for support): Version: 6.0.0 Commit: 4822e3c3aa
.NET SDKs installed: No SDKs were found.
.NET runtimes installed: Microsoft.AspNetCore.All 2.2.8 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.2.8 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.12 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.2.8 [/usr/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.12 [/usr/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download