Skip to content

fix: Disposables management for thread-safe operations #106

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

antoine-hebert
Copy link

GitHub Issue: #105

Proposed Changes

  • Bug fix
  • Feature
  • Code style update (formatting)
  • Refactoring (no functional changes, no api changes)
  • Build or CI related changes
  • Documentation content changes
  • Other, please describe:

What is the current behavior?

Adding or removing a disposable could result in an InvalidOperationException in a muti threaded context.

What is the new behavior?

Enumerating disposables while adding or removing is now thread-safe.

Impact on version

  • Major (Public API was modified.)
    • Public constructs (class, struct, delegate, enum, etc.) were removed or renamed.
    • Public members were removed or renamed.
    • Public method signatures were changed or renamed.
  • Minor (Public API was extended.)
    • Public constructs, members, or overloads were added.
  • Patch (Public API was unchanged.)
    • A bug in behavior was fixed.
    • Documentation was changed.
  • None (The library is unchanged.)
    • Only code under the build folder was changed.
    • Only code under the .github folder was changed.
    • Only code in the Benchmarks project was changed.

Checklist

Please check that your PR fulfills the following requirements:

  • Documentation has been added/updated.
  • Automated Unit / Integration tests for the changes have been added/updated.
  • Updated BREAKING_CHANGES.md (if you introduced a breaking change).
  • Your conventional commits are aligned with the Impact on version section.

Other information

@antoine-hebert antoine-hebert marked this pull request as ready for review April 3, 2025 14:41
@Soap-141
Copy link
Contributor

Soap-141 commented Apr 7, 2025

Hello, to make the commit valid, you must rename it with the correct tag fix:.

Copy link

@Copilot 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.

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

@antoine-hebert antoine-hebert changed the title Fix disposables management for thread-safe operations fix: Disposables management for thread-safe operations Apr 7, 2025
@jeanplevesque
Copy link
Member

Hi, can you provide benchmark results in your PR description for both before and after your change?
You can look at this for the format: https://github.com/nventive/Chinook.DynamicMvvm/wiki/Benchmarks-2.0.1
Thanks!


namespace Chinook.DynamicMvvm
{
public partial class ViewModelBase
{
private readonly Dictionary<string, IDisposable> _disposables = new Dictionary<string, IDisposable>();
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
private readonly ConcurrentDictionary<string, IDisposable> _disposables = new();
Copy link
Member

Choose a reason for hiding this comment

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

The way the library is currently designed doesn't imply thread safety. If we want to add thread safety as a feature, we would need to do more changes than just this one.
Extension methods such as TryGetDisposable, GetOrCreateDisposable, and their calling sites would need to be reworked with thread safety in mind because they currently have a naive implementation. (e.g. Checking if the dictionary contains an item and adding an item based on that condition is done in 2 steps rather than as an atomic operation.)

It makes sense to have thread safety in this library. However if we add it, it would need to be everywhere to be consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants