Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 24, 2025

The TaskOptions class accepted a CancellationToken parameter but it was not wired up to actually cancel tasks.

Changes

Added CancellationToken property to TaskOptions

  • Includes XML documentation with usage examples for activity cancellation and retry handler integration
  • Property flows through SubOrchestrationOptions via base constructor

Implemented cancellation in TaskOrchestrationContextWrapper

  • Pre-scheduling check: throws TaskCanceledException if token is already cancelled
  • Post-scheduling cancellation: WaitForTaskWithCancellation uses Task.WhenAny pattern to race task completion against cancellation
  • Retry handler integration: passes CancellationToken through RetryContext for imperative retry control
  • Performance optimized: skips cancellation setup for already-completed tasks

Added integration tests

  • Activity cancellation before scheduling
  • Sub-orchestrator cancellation before scheduling
  • Retry handler receives and respects cancellation token
  • Retry handler stops retrying on cancellation

Usage

// Cancel activity after timeout
using CancellationTokenSource cts = new(TimeSpan.FromMinutes(5));
TaskOptions options = new() { CancellationToken = cts.Token };

try
{
    var result = await context.CallActivityAsync<string>("MyActivity", input, options);
}
catch (TaskCanceledException)
{
    // Handle cancellation
}

// Retry handler with cancellation
TaskOptions retryOptions = new(TaskOptions.FromRetryHandler(retryContext =>
{
    if (retryContext.CancellationToken.IsCancellationRequested)
        return false; // Stop retrying
    return retryContext.LastAttemptNumber < 3;
}))
{
    CancellationToken = cts.Token
};

Notes

  • Cancellation is cooperative - does not terminate running activities/sub-orchestrators
  • Implementation is replay-safe and deterministic per orchestrator constraints
  • Parent orchestrator cancellation does not cascade to sub-orchestrations
Original prompt

This section details on the original issue you should resolve

<issue_title>Activity and sub-orchestrator cancellation support</issue_title>
<issue_description>The TaskOptions class provided when scheduling activities and sub-orchestration calls supports providing a CancellationToken. However, it's not yet wired up to actually cancel the task. This issue tracks adding support for cancellation.

  • Implement the feature for activity calls, sub-orchestrator calls, and retry handlers.
  • Add detailed <remarks> documentation with examples for how cancellation can be used.
  • Add tests that cover all the scenarios supported by task cancellation</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits December 24, 2025 19:36
…on in activities and sub-orchestrators

Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
…d fix TaskCompletionSource options

Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
Copilot AI changed the title [WIP] Add cancellation support for activities and sub-orchestrations Add cancellation support for activities, sub-orchestrators, and retry handlers Dec 24, 2025
Copilot AI requested a review from YunchuWang December 24, 2025 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Activity and sub-orchestrator cancellation support

2 participants