Skip to content

Conversation

@eugeneogongo
Copy link
Contributor

Adds Disposal Logic for IJSStreamReference in PullFromJSDataStream

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Refactor PullFromJSDataStream disposal logic and add Dispose unit tests

Description

{Detail}

Fixes #62140 (in this specific format)

@eugeneogongo eugeneogongo requested a review from a team as a code owner November 18, 2025 19:00
Copilot AI review requested due to automatic review settings November 18, 2025 19:00
@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Nov 18, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Nov 18, 2025
@dotnet-policy-service
Copy link
Contributor

Thanks for your PR, @@eugeneogongo. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds disposal logic for IJSStreamReference in PullFromJSDataStream and introduces unit tests to verify the disposal behavior. The changes refactor the stream to use a CancellationTokenSource instead of a CancellationToken directly, and implement both synchronous and asynchronous disposal patterns.

  • Replaces direct CancellationToken usage with CancellationTokenSource for better lifecycle management
  • Adds Dispose(bool disposing) and DisposeAsync() override methods to properly clean up resources
  • Introduces unit tests to verify that disposal calls propagate to the underlying IJSStreamReference

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/Components/Shared/src/PullFromJSDataStream.cs Implements disposal pattern with CancellationTokenSource management and IJSStreamReference cleanup
src/Components/WebAssembly/WebAssembly/test/PullFromJSDataStreamTest.cs Adds unit tests for synchronous and asynchronous disposal behavior, removes unused import

_streamCts?.Cancel();
try
{
_ = _jsStreamReference?.DisposeAsync().Preserve();
Copy link

Copilot AI Nov 18, 2025

Choose a reason for hiding this comment

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

The Preserve() extension method does not exist in the codebase. This will cause a compilation error.

To properly handle the fire-and-forget disposal in a synchronous context, you should use .AsTask() or simply discard the ValueTask without calling any method on it. Consider one of these alternatives:

// Option 1: Just discard the ValueTask (allows it to be collected)
_ = _jsStreamReference?.DisposeAsync();

or if you need to ensure the task runs:

// Option 2: Convert to Task (ensures the task runs)
_ = _jsStreamReference?.DisposeAsync().AsTask();
Suggested change
_ = _jsStreamReference?.DisposeAsync().Preserve();
_ = _jsStreamReference?.DisposeAsync();

Copilot uses AI. Check for mistakes.
return;
}

_streamCts?.Cancel();
Copy link

Copilot AI Nov 18, 2025

Choose a reason for hiding this comment

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

The _streamCts CancellationTokenSource is never disposed, which can lead to resource leaks. You should dispose it in both Dispose and DisposeAsync methods.

Add disposal after cancellation:

_streamCts?.Cancel();
_streamCts?.Dispose();

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PullFromJSDataStream should dispose IJSStreamReference internally

1 participant