Skip to content

ValueTask-returning benchmarks are not being called correctly #1595

Closed
@scalablecory

Description

@scalablecory

When a benchmark returns a ValueTask, it is simply called with .GetAwaiter().GetResult().

This is illegal ValueTask usage: you must not call GetResult() until the task has been completed.

IValueTaskSource-backed tasks do not need to support blocking on GetResult(). The in-box ManualResetValueTaskSourceCore does not support blocking GetResult(). Nor does the DOTNET_SYSTEM_THREADING_POOLASYNCVALUETASKS feature in .NET 5.

Instead, the ValueTask should be converted to a Task first:

static void BlockForResult(ValueTask task)
{
    if (task.IsCompleted)
        task.GetAwaiter().GetResult();
    else
        task.AsTask().GetAwaiter().GetResult();
}

(This will be unfortunate for the memory diagnoser, as a bogus Task will show up, but the alternative is to just crash...)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions