vs-threading icon indicating copy to clipboard operation
vs-threading copied to clipboard

VSTHRD103 is produced inside of ContinueWith when passing task.Result as an argument for async function.

Open MaximTrushin opened this issue 3 years ago • 5 comments

VS 2022 v17.4.2:

Steps to Reproduce:

Try to compile the following code:

...

Task<Expr.Nullable<int>> r1 = TestAsyncDelayAsync(15).ContinueWith((Task<Expr.Nullable<int>> task) => {
	return TestAsyncDelayAsync(task.**Result**.Value + 10);
}, TaskScheduler.Default).Unwrap();

var r2 = TestAsyncDelayAsync(15).ContinueWith((Task<Expr.Nullable<int>> task) => {
				return TestDelay(task.Result.Value + 10);
			}, TaskScheduler.Default);
...
...

public async Task<Expr.Nullable<int>> TestAsyncDelayAsync(int delayMilliseconds) {
	await Task.Delay(TimeSpan.FromMilliseconds(delayMilliseconds));
	return new Expr.Nullable<int>(delayMilliseconds);
}

public Expr.Nullable<int> TestDelay(int delayMilliseconds) {
	return new Expr.Nullable<int>(delayMilliseconds);
}


The assignment to r1 produces the VSTHRD103, but the assignment to r2 doesn't.

Expected Behavior: The warning must not be produced because the task is always completed when the lambda defined in ContinueWith is called.

Actual Behavior: VSTHRD103 is produced inside of ContinueWith when passing task.Result as an argument for async function.

MaximTrushin avatar Dec 09 '22 21:12 MaximTrushin

tip, @maximtrushin: use 3 backticks in a row around your multi-line code snippets so github formats them properly. Adding a cs after the opening 3 backticks adds syntax colorization.

AArnott avatar Jan 06 '23 23:01 AArnott

Can you include the actual emitted warning? I'm surprised that VSTHRD103 is doing this because I thought that was the job of VSTHRD002.
In any case though, there are several ContinueWith scenarios. This one seems similar to #1123.

AArnott avatar Jan 06 '23 23:01 AArnott

This is the emitted warning:

[VSTHRD103] Result synchronously blocks. Use await instead.

For some reason, I can't reproduce the issue when using a small console application or library assembly. I see it happening only in bigger projects.

MaximTrushin avatar Jan 10 '23 15:01 MaximTrushin