Skip to content

Return Task from TextWriter.WriteAsync/WriteLineAsync(ReadOnlyMemory<char>) - #640

Merged
SimonCropp merged 2 commits into
mainfrom
fix/textwriter-memory-task
Oct 11, 2026
Merged

SimonCropp merged 2 commits into
mainfrom
fix/textwriter-memory-task

Conversation

@SimonCropp

Copy link
Copy Markdown
Owner

Bug

The polyfills for TextWriter.WriteAsync(ReadOnlyMemory<char>, CancellationToken) and TextWriter.WriteLineAsync(ReadOnlyMemory<char>, CancellationToken) returned ValueTask. The BCL methods return Task (checked in the netcoreapp2.1, netstandard2.1 and net11 reference assemblies). Code such as Task t = writer.WriteAsync(memory);, Task.WhenAll(writer.WriteAsync(memory), ...) or .ContinueWith(...) compiled against the BCL but not against the polyfill, and because of the return type the polyfills were only available when System.Threading.Tasks.Extensions was referenced.

Fix

  • Both polyfills return Task.
  • They no longer sit behind FeatureValueTask; FeatureMemory is enough.
  • A cancelled token now produces a cancelled task (Task.FromCanceled) instead of throwing synchronously, which is what the BCL does and what the neighbouring WriteAsync(string, CancellationToken) polyfill already does.

Coverage

  • TextWriterWriteMemoryAsyncReturnsTask assigns the results to Task (does not compile against the old signatures).
  • TextWriterWriteMemoryAsyncCancelled asserts a pre-cancelled token yields a cancelled task and writes nothing.
  • Consume.cs assigns both overloads to Task and no longer requires FeatureValueTask for them.
  • Tests run on net48 (polyfill) and net10.0 (BCL); Consume built for net461, netstandard2.0, netcoreapp2.0, netcoreapp2.1 and net10.0.
  • Split files and api_list.include.md regenerated.

Note: this is a source-breaking change for callers that stored the result in a ValueTask.

@SimonCropp SimonCropp added this to the 11.4.4 milestone Oct 11, 2026
@SimonCropp
SimonCropp merged commit 01944d8 into main Oct 11, 2026
3 checks passed
@SimonCropp
SimonCropp deleted the fix/textwriter-memory-task branch October 11, 2026 22:11
This was referenced Oct 12, 2026
This was referenced Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants