Repository navigation
Fix Task WaitAsync with cancellation timeout - #410
SimonCropp wants to merge 12 commits into
Conversation
| return WaitAsyncCore(target, timeout, cancellationToken); | ||
| } | ||
|
|
||
| static async Task WaitAsyncCore(Task task, TimeSpan timeout, CancellationToken cancelToken) |
There was a problem hiding this comment.
@stephentoub if you can advise on this implementation please?
This is a polyfill for Task.WaitAsync on TFMs where it doesn't exist.
There was a problem hiding this comment.
i dont understand this statement
|
|
||
| static async Task WaitAsyncCore(Task task, TimeSpan timeout, CancellationToken cancelToken) | ||
| { | ||
| using var timeoutCancelSource = CreateTimeoutCancelSource(timeout, cancelToken, out var combinedToken); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
There was a problem hiding this comment.
Thanks!
@SimonCropp I think you can do the same as in MCP repo then.
There was a problem hiding this comment.
@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)
| // Propagate cancellation | ||
| await cancelSource.Task; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
|
|
||
| Func<Task<Task>, Task> continueFunc = completedTask => | ||
| // Register for cancellation/timeout | ||
| using (combinedToken.Register(() => cancelSource.TrySetCanceled(combinedToken), useSynchronizationContext: false)) |
There was a problem hiding this comment.
If timeout is specified, and cancellation is requested before the timeout is reached, will we throw OperationCanceledException with the wrong CancellationToken here?
There was a problem hiding this comment.
can u share a test for that scenario as a code suggestion for this pr
No description provided.