From 588faa52bd2269ca3158c2b0c9199b0b9f90ec11 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 6 Dec 2018 13:41:54 -0500 Subject: [PATCH] Fix CancellationTokenRegistration.Token after CTS.Dispose (#21394) CTR.Token should never throw, but it's currently throwing an ObjectDisposedException if the associated CancellationTokenSource has been disposed. --- .../System/Threading/CancellationTokenRegistration.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/System.Private.CoreLib/src/System/Threading/CancellationTokenRegistration.cs b/src/System.Private.CoreLib/src/System/Threading/CancellationTokenRegistration.cs index 4261b89be7c9..232a4d1c2580 100644 --- a/src/System.Private.CoreLib/src/System/Threading/CancellationTokenRegistration.cs +++ b/src/System.Private.CoreLib/src/System/Threading/CancellationTokenRegistration.cs @@ -58,7 +58,16 @@ public ValueTask DisposeAsync() /// registration isn't associated with a token (such as after the registration has been disposed), /// this will return a default token. /// - public CancellationToken Token => _node?.Partition.Source.Token ?? default; + public CancellationToken Token + { + get + { + CancellationTokenSource.CallbackNode node = _node; + return node != null ? + new CancellationToken(node.Partition.Source) : // avoid CTS.Token, which throws after disposal + default; + } + } /// /// Disposes of the registration and unregisters the target callback from the associated