Skip to content

Commit 9b46021

Browse files
CopilotYunchuWang
andcommitted
Address code review comments: optimize WaitForTaskWithCancellation and fix TaskCompletionSource options
Co-authored-by: YunchuWang <[email protected]>
1 parent 236dd30 commit 9b46021

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/Worker/Core/Shims/TaskOrchestrationContextWrapper.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,14 @@ async Task<T> WaitForTaskWithCancellation<T>(Task<T> task, CancellationToken can
556556
return await task;
557557
}
558558

559+
// If the task is already completed, return immediately without setting up cancellation infrastructure
560+
if (task.IsCompleted)
561+
{
562+
return await task;
563+
}
564+
559565
// Create a cancellation task that completes when the token is cancelled
560-
TaskCompletionSource<T> cancellationTcs = new();
566+
TaskCompletionSource<T> cancellationTcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
561567
using CancellationTokenRegistration registration = cancellationToken.Register(() =>
562568
{
563569
cancellationTcs.TrySetCanceled(cancellationToken);

0 commit comments

Comments
 (0)