From dd8e161cb21e9c7e41d57f2a55b3263a19c2ce39 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 6 Dec 2018 13:58:54 -0500 Subject: [PATCH] Fix CancellationTokenRegistration.Token after CTS.Dispose 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/mscorlib/src/System/Threading/CancellationTokenRegistration.cs b/src/mscorlib/src/System/Threading/CancellationTokenRegistration.cs index 3c964a954c37..c7a5fde99bea 100644 --- a/src/mscorlib/src/System/Threading/CancellationTokenRegistration.cs +++ b/src/mscorlib/src/System/Threading/CancellationTokenRegistration.cs @@ -41,7 +41,16 @@ public void Dispose() /// 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(CancellationToken); + 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