Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b24eee8

Browse files
authored
Fix CancellationTokenRegistration.Token after CTS.Dispose (#21417)
CTR.Token should never throw, but it's currently throwing an ObjectDisposedException if the associated CancellationTokenSource has been disposed.
1 parent 35da0c5 commit b24eee8

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/mscorlib/src/System/Threading/CancellationTokenRegistration.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,16 @@ public void Dispose()
4141
/// registration isn't associated with a token (such as after the registration has been disposed),
4242
/// this will return a default token.
4343
/// </summary>
44-
public CancellationToken Token => _node?.Partition.Source.Token ?? default(CancellationToken);
44+
public CancellationToken Token
45+
{
46+
get
47+
{
48+
CancellationTokenSource.CallbackNode node = _node;
49+
return node != null ?
50+
new CancellationToken(node.Partition.Source) : // avoid CTS.Token, which throws after disposal
51+
default;
52+
}
53+
}
4554

4655
/// <summary>
4756
/// Disposes of the registration and unregisters the target callback from the associated

0 commit comments

Comments
 (0)