-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Detect non-cancelable Task.Delay passed to Task.WhenAny #33805
Comments
Estimates:
|
I think a fixer could reasonably be done here. Also, it's such a common pattern, we could consider introducing new overloads to simplify it. |
Would a fixer simply check if the current method has a cancellationtoken parameter? Otherwise where would our cancellationtoken come from? |
The fixer would manufacture it. Essentially this: if (task != await Task.WhenAny(Task.Delay(timeout), task))
throw new TimeoutException(); would become using (var cts = new CancellationTokenSource())
{
Task t = await Task.WhenAny(Task.Delay(timeout, cts.Token), task);
cts.Cancel();
if (t != task)
throw new TimeoutException();
} or something along those lines. That said, a better approach might instead be to just add an API like #27723, as I expect that represents the 99% case for this pattern, and then a fixer here could offer to just switch to use that API if it's available. |
I believe the new API would be vastly superior as the intent is perfectly clear, while analysing the longer snippet costs some time. I would leave the fixer blank for now as the earliest an analyzer could ship appears to be .net 6 and I would hope the Task Timeout extension method becomes standardised by then. |
@Mrnikbobjeff @stephentoub this analyzer seems to be already covered with the one I wrote (CA2016), which passes a The case that analyzer CA2016 wouldn't cover is the one shared by Stephen in the example above, where we detect a newly created We could enhance CA2016 to cover that case as well. I don't think we need another analyzer for this. Anyone thinks differently? |
This is different from CA2016. In fact if CA2016 fired for the shown pattern, it would be doing the wrong thing (or at least it wouldn't be doing as well as it should). The idiom being used is simply trying to time out an await, and using a Task.Delay combined with a Task.WhenAny to do it, but then not disposing of the resources created for that timeout, namely the timer underneath the Task.Delay. So a dedicated CancellationTokenSource needs to be created that can be used to cancel the Task.Delay after the operation completes. I think the right answer here is shipping some form of #27723, and then the analyzer would flag patterns like |
So there were a bunch of APIs that got approved and them merged (Mar 11) to help with tasks and timeouts. Based on this comment:
Maybe there's more than one pattern this analyzer could potentially flag. @stephentoub @eiriktsarpalis would you mind helping determine the cases to flag and what the fixer should suggest? |
There are lots of variations to the erroneous pattern, so it might be hard to flag them all as well as come up with a good fixer. But we could probably just flag cases where Task.WhenAny(task, Task.Delay(timeout)) and suggest that it's likely an issue and Task.WaitAsync should be used instead. |
Based on the above comments the analyzer/fixer behavior could look like this:
public static void CancellationNotProvidedFlag(Task task, int timeout)
{
Task.WhenAny(task, Task.Delay(timeout)); // warn
}
public static void CancellationNotProvidedAfterFix(Task task, int timeout)
{
task.WaitAsync(TimeSpan.FromMilliseconds(timeout));
} public static void CancellationNotProvidedFlag(Task task, TimeSpan timeout)
{
Task.WhenAny(task, Task.Delay(timeout)); // warn
}
public static void CancellationNotProvidedAfterFix(Task task, TimeSpan timeout)
{
task.WaitAsync(timeout);
} public static void CancellationProvidedButNotPassedFlag(Task task, int timeout, CancellationToken token)
{
Task.WhenAny(task, Task.Delay(timeout)); // warn
}
public static void CancellationProvidedButNotPassedAfterFix(Task task, int timeout, CancellationToken token)
{
task.WaitAsync(TimeSpan.FromMilliseconds(timeout), token);
} public static void CancellationProvidedButNotPassedFlag(Task task, TimeSpan timeout, CancellationToken token)
{
Task.WhenAny(task, Task.Delay(timeout));
}
public static void CancellationProvidedButNotPassedAfterFix(Task task, TimeSpan timeout, CancellationToken token)
{
task.WaitAsync(timeout, token);
} // I assume if the `CancellationToken` is passed into `Task.Delay` as needed we should not flag
public static void CancellationProvidedAndPassedDoNotFlag(Task task, int timeout, CancellationToken token)
{
Task.WhenAny(task, Task.Delay(timeout, token)); // no warning
}
public static void CancellationProvidedAndPassedDoNotFlag(Task task, TimeSpan timeout, CancellationToken token)
{
Task.WhenAny(task, Task.Delay(timeout, token)); // no warning
} |
Seems good as proposed. Steve and Stephen pointed out that it's eventually a problem beyond performance, so the category is now Reliability.
Severity: Info |
Estimates:
|
@buyaa-n I've ran a prototype analyzer against Replacing [Fact]
public async Task ReloadingThePage_GracefullyDisconnects_TheCurrentCircuit()
{
// Arrange & Act
Browser.Navigate().Refresh();
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
// Assert
Assert.Contains((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages.ToArray());
Assert.Contains((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages.ToArray());
} But
So a simple replacement in the following cases could lead to broken code: var task = await Task.WhenAny(_allBlocksReturned.Task, Task.Delay(timeout));
if (task != _allBlocksReturned.Task) or // Verify that the response isn't flushed by verifying the TCS isn't set
var res = await Task.WhenAny(tcs.Task, Task.Delay(1000)) == tcs.Task;
Assert.False(res); or if (await Task.WhenAny(exitedTcs.Task, Task.Delay(TimeSpan.FromSeconds(TimeoutSeconds * 2))) != exitedTcs.Task)
{
try
{
process.Kill();
}
catch (Exception ex)
{
throw new TimeoutException($"h2spec didn't exit within {TimeoutSeconds * 2} seconds.", ex);
}
throw new TimeoutException($"h2spec didn't exit within {TimeoutSeconds * 2} seconds.");
} or public async Task WhenAllBlocksReturnedAsync(TimeSpan timeout)
{
var task = await Task.WhenAny(_allBlocksReturned.Task, Task.Delay(timeout));
if (task != _allBlocksReturned.Task)
{
MemoryPoolThrowHelper.ThrowInvalidOperationException_BlocksWereNotReturnedInTime(_totalBlocks - _blocks.Count, _totalBlocks, _blocks.ToArray());
}
await task;
} Most of these examples could be written more clearly with a bit more (individual) refactoring, so I am not sure if a fixer could do this automatically. Also seen in the changes in #48842. All findings
|
Flag places where a
Task.Delay
is used as an argument toWhenAny
and where thatTask.Delay
doesn't take a cancellation token, in which case theTask.Delay
is likely leaving a timer running for longer than is necessary.Category: Performance
The text was updated successfully, but these errors were encountered: