From 5c5f6710cbd5e24a66095f5c2afcf6c55215b9ce Mon Sep 17 00:00:00 2001 From: Donald Pinckney Date: Fri, 17 Apr 2026 11:37:30 -0400 Subject: [PATCH] Use CancellationToken.None for cleanup in .NET cancellation example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Creating an inert CancellationTokenSource just to get a non-cancelled token is misleading — CancellationToken.None is the idiomatic no-op. Also add a note explaining when a new detached CancellationTokenSource would actually be appropriate. Co-Authored-By: Claude Opus 4.7 (1M context) --- docs/develop/dotnet/workflows/cancellation.mdx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/develop/dotnet/workflows/cancellation.mdx b/docs/develop/dotnet/workflows/cancellation.mdx index 9ca83fffa5..0900ef0b36 100644 --- a/docs/develop/dotnet/workflows/cancellation.mdx +++ b/docs/develop/dotnet/workflows/cancellation.mdx @@ -90,15 +90,18 @@ public async Task RunAsync() // Call cleanup activity. If this throws, it will swallow the original exception which we // are ok with here. This could be changed to just log a failure and let the original - // cancellation continue. We use a different cancellation token since the default one on - // Workflow.CancellationToken is now marked cancelled. - using var detachedCancelSource = new CancellationTokenSource(); + // cancellation continue. + // The default token on Workflow.CancellationToken is now marked + // cancelled, so we pass a different one. We use CancellationToken.None here because the + // cleanup activity itself doesn't need to be cancellable; if it did (e.g. you want to + // cancel cleanup from a timeout or another signal), create a new detached + // CancellationTokenSource and pass its Token instead. await Workflow.ExecuteActivityAsync( (MyActivities a) => a.MyCancellationCleanupActivity(), new() { ScheduleToCloseTimeout = TimeSpan.FromMinutes(5), - CancellationToken = detachedCancelSource.Token; + CancellationToken = CancellationToken.None, }); // Rethrow the cancellation