-
Notifications
You must be signed in to change notification settings - Fork 53
Add cancellation support for activities, sub-orchestrators, and retry handlers #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…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>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements cancellation support for activities, sub-orchestrators, and retry handlers by wiring up the previously unused CancellationToken parameter in the TaskOptions class. The implementation provides pre-scheduling cancellation checks and post-scheduling cancellation via a Task.WhenAny pattern, enabling cooperative cancellation of orchestrator tasks.
Key changes:
- Added
CancellationTokenproperty toTaskOptionswith comprehensive XML documentation and usage examples - Implemented cancellation logic in
TaskOrchestrationContextWrapperincluding pre-scheduling checks and theWaitForTaskWithCancellationhelper method - Added four integration tests covering activity cancellation, sub-orchestrator cancellation, and retry handler integration scenarios
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/Abstractions/TaskOptions.cs | Added CancellationToken property with extensive documentation and examples, updated copy constructor to include the new property |
| src/Worker/Core/Shims/TaskOrchestrationContextWrapper.cs | Implemented cancellation logic for activities and sub-orchestrators with pre-scheduling checks, added WaitForTaskWithCancellation helper method, integrated cancellation token into retry handler flow |
| test/Grpc.IntegrationTests/CancellationTests.cs | New test file with comprehensive integration tests for activity cancellation, sub-orchestrator cancellation, and retry handler cancellation scenarios |
| instanceId, | ||
| policy.ToDurableTaskCoreRetryOptions(), | ||
| input, | ||
| input, |
Copilot
AI
Jan 7, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a Chinese full-width comma (,) used instead of a standard ASCII comma (,). This will cause a compilation error. Replace the Chinese comma with a standard comma.
| input, | |
| input, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit aa94a56. Replaced the Chinese full-width comma with a standard ASCII comma.
src/Abstractions/TaskOptions.cs
Outdated
| /// Retry = TaskOptions.FromRetryHandler(retryContext => | ||
| /// { | ||
| /// if (retryContext.CancellationToken.IsCancellationRequested) | ||
| /// { | ||
| /// return false; // Stop retrying | ||
| /// } | ||
| /// return retryContext.LastAttemptNumber < 3; | ||
| /// }), |
Copilot
AI
Jan 7, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation example shows incorrect syntax for setting the Retry property. The code assigns the result of TaskOptions.FromRetryHandler(...) to the Retry property, but this method returns a TaskOptions object, while the Retry property expects a TaskRetryOptions?. This will cause a compilation error.
The correct syntax should be to use the handler directly (which will be implicitly converted to TaskRetryOptions), or to use TaskRetryOptions.FromRetryHandler(...) instead. For example:
TaskOptions options = new TaskOptions
{
Retry = TaskRetryOptions.FromRetryHandler(retryContext =>
{
if (retryContext.CancellationToken.IsCancellationRequested)
{
return false;
}
return retryContext.LastAttemptNumber < 3;
}),
CancellationToken = cts.Token
};Or using the implicit conversion:
RetryHandler handler = retryContext =>
{
if (retryContext.CancellationToken.IsCancellationRequested)
{
return false;
}
return retryContext.LastAttemptNumber < 3;
};
TaskOptions options = new TaskOptions
{
Retry = handler, // Implicit conversion from RetryHandler to TaskRetryOptions
CancellationToken = cts.Token
};There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit aa94a56. Updated the documentation example to use TaskRetryOptions.FromRetryHandler() instead of TaskOptions.FromRetryHandler(), which correctly returns TaskRetryOptions as expected by the Retry property.
- Replace Chinese full-width comma with ASCII comma in TaskOrchestrationContextWrapper.cs - Fix documentation example to use TaskRetryOptions.FromRetryHandler instead of TaskOptions.FromRetryHandler Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
|
@copilot address the comment |
|
@copilot why pr shows full file of src/Abstractions/TaskOptions.cs is changed, thats not right, fix |
The previous commits inadvertently changed all line endings from CRLF to LF, causing the entire file to show as modified. This commit restores CRLF line endings while keeping all the functional changes (CancellationToken property and documentation). Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
Fixed in commit f68ff62. The issue was that the previous commits inadvertently changed line endings from CRLF to LF, causing git to show the entire file as modified. I've restored the CRLF line endings while keeping all functional changes. The PR now shows only 62 lines added (the actual CancellationToken property and documentation) instead of the full file replacement. |
Activity and Sub-orchestrator Cancellation Support
This PR implements cancellation support for activities, sub-orchestrators, and retry handlers through the
TaskOptionsclass, addressing issue #7.Implementation Complete
CancellationTokenproperty toTaskOptionsandSubOrchestrationOptionsclassesTaskOrchestrationContextWrapper.CallActivityAsyncmethodTaskOrchestrationContextWrapper.CallSubOrchestratorAsyncmethodTaskOrchestrationContextWrapper.InvokeWithCustomRetryHandlermethodSummary of Changes
1. Added
CancellationTokenProperty toTaskOptionsCancellationTokenproperty with detailed XML documentation2. Implemented Cancellation in
TaskOrchestrationContextWrapperCallActivityAsync:
WaitForTaskWithCancellationto handle cancellation while waitingCallSubOrchestratorAsync:
WaitForTaskWithCancellationfor waitingInvokeWithCustomRetryHandler:
RetryContextWaitForTaskWithCancellation:
TaskCreationOptions.RunContinuationsAsynchronouslyto prevent deadlocks3. Created Integration Tests
Four comprehensive tests in
CancellationTests.cs:ActivityCancellation- Tests cancelling before activity startsSubOrchestratorCancellation- Tests cancelling before sub-orchestrator startsRetryHandlerReceivesCancellationToken- Verifies token is passed to handlerRetryHandlerCanStopOnCancellation- Tests handler can stop retrying on cancellationAll tests pass successfully.
Recent Fixes
TaskRetryOptions.FromRetryHandlerinstead ofTaskOptions.FromRetryHandlerDesign Notes
Security Summary
No security vulnerabilities were introduced by these changes. The implementation:
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.