-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Comments
Here's a link to where the sample code for detecting the Transient Disposables was added dotnet/AspNetCore.Docs#18786 |
@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 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? |
@oatsoda yep. |
@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? |
@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. |
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. |
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. |
Is there an existing issue for this?
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
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 theAuthenticationStateChanged
.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
The text was updated successfully, but these errors were encountered: