Skip to content

Clarify ValueTask consumption guidance#12751

Open
YNan-varamor wants to merge 1 commit into
dotnet:mainfrom
YNan-varamor:docs/12704-valuetask-consumption-guidance
Open

Clarify ValueTask consumption guidance#12751
YNan-varamor wants to merge 1 commit into
dotnet:mainfrom
YNan-varamor:docs/12704-valuetask-consumption-guidance

Conversation

@YNan-varamor

Copy link
Copy Markdown

Fixes #12704.

This updates the ValueTask and ValueTask<TResult> remarks so the guidance is less absolute and reflects the backing-source distinction:

  • instances backed by Task or a synchronous result may allow repeated awaits
  • arbitrary ValueTask instances should still be treated as single-consumption because they may be backed by pooled IValueTaskSource
  • AsTask remains the documented path when multiple consumption is needed

Validation:

  • git diff --check
  • confirmed the previous may-only-be-awaited-once wording no longer appears in the ValueTask XML docs

Disclosure: I used OpenAI Codex to implement and verify this documentation update.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jun 16, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-threading-tasks

@YNan-varamor

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@YNan-varamor YNan-varamor marked this pull request as ready for review June 17, 2026 05:05
@YNan-varamor YNan-varamor requested a review from a team as a code owner June 17, 2026 05:05
Copilot AI review requested due to automatic review settings June 17, 2026 05:05

Copilot AI left a comment

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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates the ValueTask / ValueTask<T> reference docs to better reflect real-world behavior (Task-backed vs pooled IValueTaskSource) and to soften “never do this” guidance into conditional guidance based on provider support.

Changes:

  • Clarify that multiple awaits may appear to work for Task/synchronous-backed ValueTask instances, but pooled IValueTaskSource-backed instances should be treated as single-consumption unless documented otherwise.
  • Update the “should never be performed” list to “should not be performed unless supported by provider” for both ValueTask and ValueTask<T>.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
xml/System.Threading.Tasks/ValueTask`1.xml Refines ValueTask<T> remarks about repeated awaits and IValueTaskSource<T> pooling semantics.
xml/System.Threading.Tasks/ValueTask.xml Mirrors the same guidance updates for non-generic ValueTask.


## Remarks
A `ValueTask` instance may either be awaited or converted to a <xref:System.Threading.Tasks.Task> using <xref:System.Threading.Tasks.ValueTask.AsTask*>. A `ValueTask` instance may only be awaited once, and consumers may not call <xref:System.Threading.Tasks.ValueTask.GetAwaiter> until the instance has completed. If these limitations are unacceptable, convert the `ValueTask` to a <xref:System.Threading.Tasks.Task> by calling <xref:System.Threading.Tasks.ValueTask.AsTask*>.
A `ValueTask` instance may either be awaited or converted to a <xref:System.Threading.Tasks.Task> using <xref:System.Threading.Tasks.ValueTask.AsTask*>. If the instance wraps a <xref:System.Threading.Tasks.Task> or a synchronous result, multiple awaits might succeed. However, a `ValueTask` instance can also be backed by a pooled <xref:System.Threading.Tasks.Sources.IValueTaskSource>, so consumers should treat an arbitrary `ValueTask` as single-consumption unless the provider explicitly documents support for multiple consumption. Consumers may not call <xref:System.Threading.Tasks.ValueTask.GetAwaiter> until the instance has completed. If these limitations are unacceptable, convert the `ValueTask` to a <xref:System.Threading.Tasks.Task> by calling <xref:System.Threading.Tasks.ValueTask.AsTask*>.
Comment on lines +93 to +95
A <xref:System.Threading.Tasks.ValueTask`1> instance may either be awaited or converted to a <xref:System.Threading.Tasks.Task`1> using <xref:System.Threading.Tasks.ValueTask`1.AsTask*>. If the instance wraps a <xref:System.Threading.Tasks.Task`1> or a synchronous result, multiple awaits might succeed. However, a <xref:System.Threading.Tasks.ValueTask`1> instance can also be backed by a pooled <xref:System.Threading.Tasks.Sources.IValueTaskSource`1>, so consumers should treat an arbitrary <xref:System.Threading.Tasks.ValueTask`1> as single-consumption unless the provider explicitly documents support for multiple consumption. Consumers may not read <xref:System.Threading.Tasks.ValueTask`1.Result> until the instance has completed. If these limitations are unacceptable, convert the <xref:System.Threading.Tasks.ValueTask`1> to a <xref:System.Threading.Tasks.Task`1> by calling <xref:System.Threading.Tasks.ValueTask`1.AsTask*>.

The following operations should never be performed on a <xref:System.Threading.Tasks.ValueTask`1> instance:
The following operations should not be performed on a <xref:System.Threading.Tasks.ValueTask`1> instance unless the provider explicitly documents that they are supported:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Threading.Tasks community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValueTask<TResult> documentation is outdated for .NET 10 - repeated await no longer throws

2 participants