Identityserver4: How to prevent logging for EnableTokenCleanup

Created on 17 Mar 2020  路  5Comments  路  Source: IdentityServer/IdentityServer4

Question

Has anyone run into an issue where TokenCleanupHost.StartInternalAsync causes thread starvation?

Minimal working example

I have a token server running the latest version of IdentityServer4 on .net core 3.1 and using EntityFramework for the operational store only (codes, tokens, consents).

The configuration for that looks like this:

                   .AddOperationalStore(options => {
                    options.ConfigureDbContext = builder =>
                        builder.UseSqlServer(ssoConnection,
                            sqlOptions => {
                                sqlOptions.MigrationsAssembly(migrationsAssembly);
                                sqlOptions.EnableRetryOnFailure();
                            });
                    // this enables automatic token cleanup. this is optional.
                    options.EnableTokenCleanup = true;
                    options.TokenCleanupInterval = 30;
                })

Relevant debugging information

Over time, we can watch the thread count to rise until the application crashes due to thread starvation.

When we take a memory dump from the thread-starved process, we see hundreds of instances of threads waiting on Application Insights logging. We think they are rooted by the calls to TokenCleanupHost.StartInternalAsync although truthfully I am not enough of an expert to say for sure.

here's the tasks view of the memory dump

image

and here's the threading view showing the App Insights logging threads apparently waiting

image

My guess is that we have App Insights logging configured incorrectly but even stripping it down to what I think matches guidance, I still see the thread starvation issue.

Wondering if anyone else has run into this or has advice.

question

Most helpful comment

TokenCleanupHost.StartInternalAsync was not the cause for this issue. It was just a casualty. This was corrected by eliminating the per-request calls to BuildServiceProvider() in the Startup.cs. Apologies for the distraction.

All 5 comments

Bizarre. I'd almost ask them (meaning AppInsights or MSFT) what's going on? The background cleanup is only working on a single (logical) thread, so I don't why app insights is doing their own? I'd suggest removing app insights to see if the problem persists (since you can repo it). That'd narrow down whose issue it is :)

I removed Application Insights so I'll keep an eye on it for the next few days.

I received some help from Microsoft and it sounds like the real issue here is multiple calls to BuildServiceProvider in startup and some need for better object disposal. I will close this out once I confirm.

TokenCleanupHost.StartInternalAsync was not the cause for this issue. It was just a casualty. This was corrected by eliminating the per-request calls to BuildServiceProvider() in the Startup.cs. Apologies for the distraction.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eshorgan picture eshorgan  路  3Comments

ekarlso picture ekarlso  路  3Comments

leksim picture leksim  路  3Comments

leastprivilege picture leastprivilege  路  3Comments

mackie1001 picture mackie1001  路  3Comments