Skip to content

Make Disposable.Create produce idempotent disposables - #95

Merged
Tyrrrz merged 5 commits into
primefrom
copilot/disposable-create-idempotent-disposables
Jul 26, 2026
Merged

Make Disposable.Create produce idempotent disposables#95
Tyrrrz merged 5 commits into
primefrom
copilot/disposable-create-idempotent-disposables

Conversation

Copilot AI commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Disposable.Create previously returned a disposable that invoked the delegate on every Dispose() call. The produced disposable should only invoke the action once, regardless of how many times it is disposed.

Changes

  • DelegateDisposable: uses Interlocked.Exchange on an int flag to ensure the delegate fires exactly once, with thread-safety guarantees
  • DisposableTests: adds Create_Idempotent_Test asserting that repeated Dispose() calls invoke the action only once
var count = 0;
var disposable = Disposable.Create(() => Interlocked.Increment(ref count));

disposable.Dispose();
disposable.Dispose();
disposable.Dispose();

count == 1; // true

@Tyrrrz Tyrrrz added the bug Something isn't working label Jul 26, 2026
@Tyrrrz
Tyrrrz marked this pull request as ready for review July 26, 2026 19:34
Copilot AI review requested due to automatic review settings July 26, 2026 19:34
@Tyrrrz

Tyrrrz commented Jul 26, 2026

Copy link
Copy Markdown
Owner

@copilot let's mention the idempotency in remarks in xmldocs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 changes Disposable.Create to return an idempotent IDisposable, ensuring the provided delegate is invoked at most once even if Dispose() is called multiple times (including from multiple threads).

Changes:

  • Updated the internal DelegateDisposable to gate invocation with an Interlocked flag so disposal happens exactly once.
  • Added a new unit test verifying that repeated Dispose() calls only invoke the action once.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
PowerKit/Disposable.cs Makes Disposable.Create-backed disposables idempotent via an Interlocked-guarded DelegateDisposable.
PowerKit.Tests/DisposableTests.cs Adds a regression test asserting multiple Dispose() calls only trigger the action once.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread PowerKit/Disposable.cs Outdated

Copilot AI commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

@copilot let's mention the idempotency in remarks in xmldocs

Updated in 1c28146Disposable.Create now documents idempotency in XML docs remarks.

Copilot AI requested a review from Tyrrrz July 26, 2026 19:39
@codecov

codecov Bot commented Jul 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.52%. Comparing base (6ff6600) to head (b390b2f).

Additional details and impacted files
@@           Coverage Diff           @@
##            prime      #95   +/-   ##
=======================================
  Coverage   86.51%   86.52%           
=======================================
  Files          78       78           
  Lines        1409     1410    +1     
  Branches      242      243    +1     
=======================================
+ Hits         1219     1220    +1     
  Misses        137      137           
  Partials       53       53           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants