dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

AsyncCollectionRequestMessage<T> creates a CancellationTokenSource that never gets disposed

Open jphorv-bdo opened this issue 5 months ago • 1 comments

Describe the bug

I was examining the code for the the AsyncCollectionRequestMessage<T> class and I noticed it creates a private CancellationTokenSource that never gets Dispose() called on it.

I looked at CancellationTokenSource's dispose methods and it's non-trivial, potentially disposing of a System.ITimer and a ManualResetEvent - both things that are potentially finite resources, so better to release ASAP.

Regression

No response

Steps to reproduce

No reproduction, just observation of the code.

Expected behavior

There should be an opportunity to dispose the CancellationTokenSource. Most obvious possible way would be to make thing that comes to mind would be make AsyncCollectionRequestMessage<T> implement IDispose so that users could call it.

Screenshots

No response

IDE and version

VS 2022

IDE version

17.14.11

Nuget packages

  • [ ] CommunityToolkit.Common
  • [ ] CommunityToolkit.Diagnostics
  • [ ] CommunityToolkit.HighPerformance
  • [x] CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.4.0

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

jphorv-bdo avatar Aug 08 '25 20:08 jphorv-bdo

The issue you referenced is:

Title: AsyncCollectionRequestMessage<T> creates a CancellationTokenSource that never gets disposed
Repository: CommunityToolkit/dotnet
Issue #: 1117
Status: Open
Author: jphorv-bdo
Labels: bug :bug:
Created: 6 days ago

Description:
The issue points out that the AsyncCollectionRequestMessage<T> class creates a private CancellationTokenSource but never calls Dispose() on it. This is problematic because CancellationTokenSource.Dispose() releases resources such as System.ITimer and ManualResetEvent, which are finite and should be freed as soon as possible.

Suggested Solution:
The author suggests that AsyncCollectionRequestMessage<T> could implement IDisposable so that users could dispose of it properly.

Environment:

  • IDE: Visual Studio 2022 (v17.14.11)
  • NuGet package: CommunityToolkit.Mvvm (8.4.0)

Additional Notes:

  • No reproduction steps or screenshots were provided—this was an observation from code review.
  • The author volunteered to be assigned to work on the issue.

You can view the issue directly here: AsyncCollectionRequestMessage<T> creates a CancellationTokenSource that never gets disposed

treaant677792 avatar Aug 14 '25 20:08 treaant677792