-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
I analyzed the source code of ASP.NET Core v6.0.18 using a Svace static analyzer. He found an error of category HANDLE_LEAK with the following message
CancellationTokenSource.CreateLinkedTokenSource(a, b) is not disposed at the end of the function
in method GetLinkedCancellationToken()
. Here's a source
aspnetcore/src/Components/Server/src/Circuits/RemoteJSDataStream.cs
Lines 190 to 202 in 28b2bfd
private static CancellationToken GetLinkedCancellationToken(CancellationToken a, CancellationToken b) | |
{ | |
if (a.CanBeCanceled && b.CanBeCanceled) | |
{ | |
return CancellationTokenSource.CreateLinkedTokenSource(a, b).Token; | |
} | |
else if (a.CanBeCanceled) | |
{ | |
return a; | |
} | |
return b; | |
} |
First of all, its useful to note, that method CancellationTokenSource.CreateLinkedTokenSource()
(link below) already has its own all necessary checks that are implemented in the method above
Moreover, calling CreateLinkedTokenSource()
actually creates instance of TokenSource
, which can be disposed
Proposed update
This method is applied only twice in this class: in methods
aspnetcore/src/Components/Server/src/Circuits/RemoteJSDataStream.cs
Lines 178 to 182 in 28b2bfd
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | |
{ | |
var linkedCancellationToken = GetLinkedCancellationToken(_streamCancellationToken, cancellationToken); | |
return await _pipeReaderStream.ReadAsync(buffer.AsMemory(offset, count), linkedCancellationToken); | |
} |
and
aspnetcore/src/Components/Server/src/Circuits/RemoteJSDataStream.cs
Lines 184 to 188 in 28b2bfd
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) | |
{ | |
var linkedCancellationToken = GetLinkedCancellationToken(_streamCancellationToken, cancellationToken); | |
return await _pipeReaderStream.ReadAsync(buffer, linkedCancellationToken); | |
} |
and I think, these methods can be improved with using using
construction and removing GetLinkedCancellationToken()
like this:
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
using var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(_streamCancellationToken, cancellationToken).Token;
return await _pipeReaderStream.ReadAsync(buffer.AsMemory(offset, count), linkedCancellationToken);
}
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
using var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(_streamCancellationToken, cancellationToken).Token;
return await _pipeReaderStream.ReadAsync(buffer, linkedCancellationToken);
}
Found by Linux Verification Center (linuxtesting.org) with SVACE.
Reporter: Aleksey Kolosov ([email protected]).
Organization: [email protected]