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

Transient "disposable" services language #31726

Closed
guardrex opened this issue Feb 7, 2024 · 2 comments · Fixed by #31738
Closed

Transient "disposable" services language #31726

guardrex opened this issue Feb 7, 2024 · 2 comments · Fixed by #31738

Comments

@guardrex
Copy link
Collaborator

guardrex commented Feb 7, 2024

Description

Per discussion with @hakenr at #31705 (comment), make further updates to the lead-in remarks of the OwningComponentBase coverage ...

https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-8.0#utility-base-component-classes-to-manage-a-di-scope

Perhaps, add additional remarks to explain what is meant by "disposables." I can add a cross-link to the PU issue that @hakenr found.

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-8.0#utility-base-component-classes-to-manage-a-di-scope

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/fundamentals/dependency-injection.md

Document ID

d9aabe95-69e9-0a03-81ff-429914137f9d

Article author

@guardrex

@hakenr
Copy link
Contributor

hakenr commented Feb 8, 2024

As per my understanding the disposability brings much more pain than just plain transient service:

https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#idisposable-guidance-for-transient-and-shared-instances

https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#disposable-transient-services-captured-by-container

When you register Transient services that implement IDisposable, by default the DI container will hold onto these references, and not Dispose() of them until the container is disposed when application stops if they were resolved from the container, or until the scope is disposed if they were resolved from a scope. This can turn into a memory leak if resolved from container level.

I believe regular (non-disposable) transient services are not tracked by the DI container; they get garbage collected.

https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#idisposable-guidance-for-transient-and-shared-instances

Is there a specific behavior of transient disposables in Blazor so that we want to emphasize the need to avoid them?
Maybe this issue helps to understand the impact: dotnet/aspnetcore#26676

@guardrex
Copy link
Collaborator Author

guardrex commented Feb 8, 2024

Indeed! I see now why Javier was so specific in his choice of words, i.e., "transient disposables" ...

The DI container for Blazor Server cannot be garbage collected until the circuit is terminated. Consequently, transient services implementing IDisposable are kept in memory till circuit termination.

Given the lifetime of the circuit may be much longer than the lifetime of the component, we're unduly delaying the disposal of transient services implementing IDisposable, thus leaking memory.

So, I'll revise the language to include additional explanation, and I can cross-link that PU issue, which explains this out in detail and the possible design under consideration for .NET 9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants