From 3d41ee449aff1146de235a9d04c6d9431f5054a7 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 (dotnet/coreclr#21394) CTR.Token should never throw, but it's currently throwing an ObjectDisposedException if the associated CancellationTokenSource has been disposed. Commit migrated from https://github.com/dotnet/coreclr/commit/588faa52bd2269ca3158c2b0c9199b0b9f90ec11 --- .../src/System/Threading/CancellationTokenRegistration.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Threading/CancellationTokenRegistration.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Threading/CancellationTokenRegistration.cs index 4261b89..232a4d1 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Threading/CancellationTokenRegistration.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Threading/CancellationTokenRegistration.cs @@ -58,7 +58,16 @@ namespace System.Threading /// 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 -- 2.7.4