Skip to content

Fix Task WaitAsync with cancellation timeout - #410

Closed
SimonCropp wants to merge 12 commits into
sdk10Versionfrom
Task_WaitAsync_With_Cancellation_Timeout
Closed

SimonCropp wants to merge 12 commits into
sdk10Versionfrom
Task_WaitAsync_With_Cancellation_Timeout

Conversation

@SimonCropp

Copy link
Copy Markdown
Owner

No description provided.

@SimonCropp
SimonCropp changed the base branch from main to sdk10Version November 4, 2025 20:00
@SimonCropp SimonCropp changed the title Task wait async with cancellation timeout Fix Task WaitAsync with cancellation timeout Nov 4, 2025
return WaitAsyncCore(target, timeout, cancellationToken);
}

static async Task WaitAsyncCore(Task task, TimeSpan timeout, CancellationToken cancelToken)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephentoub if you can advise on this implementation please?
This is a polyfill for Task.WaitAsync on TFMs where it doesn't exist.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont understand this statement


static async Task WaitAsyncCore(Task task, TimeSpan timeout, CancellationToken cancelToken)
{
using var timeoutCancelSource = CreateTimeoutCancelSource(timeout, cancelToken, out var combinedToken);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it's simpler to use await Task.WhenAny(task, Task.Delay(timeout, cancellationToken))

If the returned task is task, then it ran successfully. Otherwise, it's either timeout or cancellation, depending on whether IsCancellationRequested flag is true or not.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u try that in a new PR that targets this branch to see if it passes the same test that exist in this branch

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@SimonCropp I think you can do the same as in MCP repo then.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Youssef1313 PR to this branch please

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimonCropp I opened #414. Same tests that you added here, but mostly using the logic from MCP that Stephen pointed out to (adjusted it a little bit for better parity with .NET implementation)

Comment on lines +85 to +86
// Propagate cancellation
await cancelSource.Task;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you verified what is the behavior of the original Task.WaitAsync when cancellation is requested, but the original task later throws an exception? Does it surface as unobserved task exception?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Func<Task<Task>, Task> continueFunc = completedTask =>
// Register for cancellation/timeout
using (combinedToken.Register(() => cancelSource.TrySetCanceled(combinedToken), useSynchronizationContext: false))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If timeout is specified, and cancellation is requested before the timeout is reached, will we throw OperationCanceledException with the wrong CancellationToken here?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u share a test for that scenario as a code suggestion for this pr

@SimonCropp SimonCropp closed this Nov 11, 2025
@SimonCropp
SimonCropp deleted the Task_WaitAsync_With_Cancellation_Timeout branch December 17, 2025 20:54
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.

3 participants