Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor Wasm Transient Disposable registrations of AuthorizationMessageHandler #43234

Closed
1 task done
oatsoda opened this issue Aug 12, 2022 · 8 comments
Closed
1 task done
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved

Comments

@oatsoda
Copy link

oatsoda commented Aug 12, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

As I understand it, in Blazor Wasm, registering Disposable dependencies with Transient lifetimes causes memory leaks as every instance of the dependency created throughout the lifetime of the app will never be garbage collected due to the DI Container maintaining a reference to each instance (presumably as part of its need to Dispose the instance; though why it doesn't release the reference after Disposing, I'm not quite sure).

There's even some sample code on how to detect if you have accidentally registered some Transient Disposables:
https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-6.0#detect-transient-disposables-in-blazor-server-apps

I've just run some very similar code on my app to check and was surprised to find that there are a couple of Blazor classes which are Disposable being registered as Transient:

https://github.com/dotnet/aspnetcore/blob/main/src/Components/WebAssembly/WebAssembly.Authentication/src/WebAssemblyAuthenticationServiceCollectionExtensions.cs#L43-L44

services.TryAddTransient<BaseAddressAuthorizationMessageHandler>();
services.TryAddTransient<AuthorizationMessageHandler>();

Which presumably means throughout the lifetime of the app these are never being garbage collected?

Expected Behavior

As delegate handlers I guess these must be Transient, so it would mean that the AuthorizationMessageHandler would need another mechanism of unsubscribing from the AuthenticationStateChanged.

Unless of course, my understanding of Transient Disposables is incorrect?

Steps To Reproduce

Apply some code, such as here to detect Transient Disposable classes and run your Blazor Wasm app - and observe that the above two types are detected.

Exceptions (if any)

No response

.NET Version

6.0.302

Anything else?

No response

@oatsoda
Copy link
Author

oatsoda commented Aug 12, 2022

Here's a link to where the sample code for detecting the Transient Disposables was added dotnet/AspNetCore.Docs#18786

@javiercn
Copy link
Member

javiercn commented Aug 12, 2022

@oatsoda thanks for contacting us.

Your assessment is correct. However, this is a requirement for the handlers to be used with HttpClientFactory, which creates its own DI scopes, so it is not a problem in that case.

It can be a problem if you use them from the global scope, but that is it.

@javiercn javiercn added area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly question ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. labels Aug 12, 2022
@ghost ghost added the Status: Resolved label Aug 12, 2022
@oatsoda
Copy link
Author

oatsoda commented Aug 12, 2022

@javiercn Thanks. OK, so the HttpClientFactory will dispose the Scope at which point it would remove all of the references to the disposable transients - but presumably during the lifetime of that Scope, those instances which are no longer used still aren't garbage collected?

@javiercn
Copy link
Member

@oatsoda yep.

@oatsoda
Copy link
Author

oatsoda commented Aug 12, 2022

@javiercn What's the likely timespan of the Scope on the HttpClientFactory? We're talking more like hours than seconds, right?

Or am I getting confused? Many HttpClients and their Delegate Handlers can be created within that scope in the HttpClientFactory? Or is the scope created per HttpClient?

@javiercn
Copy link
Member

@oatsoda the lifetime I believe is configurable, but I believe is between 2 and 5 minutes. Once HttpClientFactory creates an instance, it reuses it for the lifetime of the scope, I believe.

@ghost
Copy link

ghost commented Aug 13, 2022

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed Aug 13, 2022
@davidfowl davidfowl reopened this Aug 13, 2022
@ghost
Copy link

ghost commented Aug 14, 2022

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed Aug 14, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 13, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants