Skip to content
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

Fix: Dispose CancellationTokenSource Properly Within CancellationDisposable #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace UniRx
public sealed class CancellationDisposable : ICancelable
{
private readonly CancellationTokenSource _cts;
private readonly bool _isCtsInternal;

/// <summary>
/// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.CancellationDisposable"/> class that uses an existing <seealso cref="T:System.Threading.CancellationTokenSource"/>.
Expand All @@ -36,6 +37,7 @@ public CancellationDisposable(CancellationTokenSource cts)
public CancellationDisposable()
: this(new CancellationTokenSource())
{
_isCtsInternal = true;
}

/// <summary>
Expand All @@ -51,7 +53,15 @@ public CancellationToken Token
/// </summary>
public void Dispose()
{
_cts.Cancel();
if (!IsDisposed)
{
_cts.Cancel();
}
Copy link
Author

Choose a reason for hiding this comment

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

The Dispose() method is designed to be idempotent, allowing for multiple invocations. If a CancellationTokenSource object is created inside CancellationDisposable and gets disposed during the initial call to Dispose(), subsequent attempts to cancel it will result in an exception.


if (_isCtsInternal)
{
_cts.Dispose();
}
}

/// <summary>
Expand Down