-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Adds Disposal Logic for IJSStreamReference in PullFromJSDataStream #64426
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,8 +14,9 @@ internal sealed class PullFromJSDataStream : Stream | |||||
| private readonly IJSRuntime _runtime; | ||||||
| private readonly IJSStreamReference _jsStreamReference; | ||||||
| private readonly long _totalLength; | ||||||
| private readonly CancellationToken _streamCancellationToken; | ||||||
| private readonly CancellationTokenSource _streamCts; | ||||||
| private long _offset; | ||||||
| private bool _isDisposed; | ||||||
|
|
||||||
| public static PullFromJSDataStream CreateJSDataStream( | ||||||
| IJSRuntime runtime, | ||||||
|
|
@@ -36,8 +37,9 @@ private PullFromJSDataStream( | |||||
| _runtime = runtime; | ||||||
| _jsStreamReference = jsStreamReference; | ||||||
| _totalLength = totalLength; | ||||||
| _streamCancellationToken = cancellationToken; | ||||||
| _streamCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); | ||||||
| _offset = 0; | ||||||
|
|
||||||
| } | ||||||
|
|
||||||
| public override bool CanRead => true; | ||||||
|
|
@@ -88,7 +90,7 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation | |||||
| private void ThrowIfCancellationRequested(CancellationToken cancellationToken) | ||||||
| { | ||||||
| if (cancellationToken.IsCancellationRequested || | ||||||
| _streamCancellationToken.IsCancellationRequested) | ||||||
| _streamCts.IsCancellationRequested) | ||||||
| { | ||||||
| throw new TaskCanceledException(); | ||||||
| } | ||||||
|
|
@@ -110,4 +112,47 @@ private async ValueTask<byte[]> RequestDataFromJSAsync(int numBytesToRead) | |||||
| } | ||||||
| return bytesRead; | ||||||
| } | ||||||
|
|
||||||
| protected override void Dispose(bool disposing) | ||||||
| { | ||||||
| if (_isDisposed) | ||||||
| { | ||||||
| return; | ||||||
| } | ||||||
| _streamCts?.Cancel(); | ||||||
| try | ||||||
| { | ||||||
| _ = _jsStreamReference?.DisposeAsync().Preserve(); | ||||||
|
||||||
| _ = _jsStreamReference?.DisposeAsync().Preserve(); | |
| _ = _jsStreamReference?.DisposeAsync(); |
eugeneogongo marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
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();
Uh oh!
There was an error while loading. Please reload this page.