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

Ensure ComponentState is always disposed #48406

Merged
merged 3 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Components/Components/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Microsoft.AspNetCore.Components.ParameterView.ToDictionary() -> System.Collectio
Microsoft.AspNetCore.Components.RenderHandle.DispatchExceptionAsync(System.Exception! exception) -> System.Threading.Tasks.Task!
*REMOVED*Microsoft.AspNetCore.Components.NavigationManager.ToAbsoluteUri(string! relativeUri) -> System.Uri!
Microsoft.AspNetCore.Components.NavigationManager.ToAbsoluteUri(string? relativeUri) -> System.Uri!
Microsoft.AspNetCore.Components.Rendering.ComponentState.DisposeAsync() -> System.Threading.Tasks.ValueTask
Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.SetEventHandlerName(string! eventHandlerName) -> void
*REMOVED*Microsoft.AspNetCore.Components.RouteData.RouteData(System.Type! pageType, System.Collections.Generic.IReadOnlyDictionary<string!, object!>! routeValues) -> void
*REMOVED*Microsoft.AspNetCore.Components.RouteData.RouteValues.get -> System.Collections.Generic.IReadOnlyDictionary<string!, object!>!
Expand All @@ -34,7 +35,6 @@ Microsoft.AspNetCore.Components.Rendering.ComponentState
Microsoft.AspNetCore.Components.Rendering.ComponentState.Component.get -> Microsoft.AspNetCore.Components.IComponent!
Microsoft.AspNetCore.Components.Rendering.ComponentState.ComponentId.get -> int
Microsoft.AspNetCore.Components.Rendering.ComponentState.ComponentState(Microsoft.AspNetCore.Components.RenderTree.Renderer! renderer, int componentId, Microsoft.AspNetCore.Components.IComponent! component, Microsoft.AspNetCore.Components.Rendering.ComponentState? parentComponentState) -> void
Microsoft.AspNetCore.Components.Rendering.ComponentState.Dispose() -> void
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a breaking change because ComponentState is only becoming public in 8.0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

Microsoft.AspNetCore.Components.Rendering.ComponentState.ParentComponentState.get -> Microsoft.AspNetCore.Components.Rendering.ComponentState?
Microsoft.AspNetCore.Components.RenderTree.Renderer.GetComponentState(int componentId) -> Microsoft.AspNetCore.Components.Rendering.ComponentState!
Microsoft.AspNetCore.Components.Sections.SectionContent
Expand Down
38 changes: 12 additions & 26 deletions src/Components/Components/src/RenderTree/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,39 +1080,25 @@ protected virtual void Dispose(bool disposing)
{
Log.DisposingComponent(_logger, componentState);

// Components shouldn't need to implement IAsyncDisposable and IDisposable simultaneously,
// but in case they do, we prefer the async overload since we understand the sync overload
// is implemented for more "constrained" scenarios.
// Component authors are responsible for their IAsyncDisposable implementations not taking
// forever.
if (componentState.Component is IAsyncDisposable asyncDisposable)
try
{
try
var task = componentState.DisposeAsync();
if (task.IsCompletedSuccessfully)
{
var task = asyncDisposable.DisposeAsync();
if (!task.IsCompletedSuccessfully)
{
asyncDisposables ??= new();
asyncDisposables.Add(task.AsTask());
}
// If it's a IValueTaskSource backed ValueTask,
// inform it its result has been read so it can reset
task.GetAwaiter().GetResult();
}
catch (Exception exception)
else
{
exceptions ??= new List<Exception>();
exceptions.Add(exception);
asyncDisposables ??= new();
asyncDisposables.Add(task.AsTask());
}
}
else if (componentState.Component is IDisposable disposable)
catch (Exception exception)
{
try
{
componentState.Dispose();
}
catch (Exception exception)
{
exceptions ??= new List<Exception>();
exceptions.Add(exception);
}
exceptions ??= new List<Exception>();
exceptions.Add(exception);
}
}

Expand Down
20 changes: 15 additions & 5 deletions src/Components/Components/src/Rendering/ComponentState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Components.Rendering;
/// detail of <see cref="Renderer"/>.
/// </summary>
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
public class ComponentState : IDisposable
public class ComponentState : IAsyncDisposable
SteveSandersonMS marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly Renderer _renderer;
private readonly IReadOnlyList<CascadingParameterState> _cascadingParameters;
Expand Down Expand Up @@ -254,15 +254,25 @@ private void RemoveCascadingParameterSubscriptions()
}

/// <summary>
/// Disposes this instance.
/// Disposes this instance and its associated component.
/// </summary>
public void Dispose()
public ValueTask DisposeAsync()
{
DisposeBuffers();

if (Component is IDisposable disposable)
// Components shouldn't need to implement IAsyncDisposable and IDisposable simultaneously,
// but in case they do, we prefer the async overload since we understand the sync overload
// is implemented for more "constrained" scenarios.
// Component authors are responsible for their IAsyncDisposable implementations not taking
// forever.
if (Component is IAsyncDisposable asyncDisposable)
{
disposable.Dispose();
return asyncDisposable.DisposeAsync();
}
else
{
(Component as IDisposable)?.Dispose();
return ValueTask.CompletedTask;
}
}

Expand Down