Fix intermittent NullReferenceException in Utf8BomInsertingStream by overriding ReadAsync#127453
Draft
Fix intermittent NullReferenceException in Utf8BomInsertingStream by overriding ReadAsync#127453
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
…nstructor_Throws Override ReadAsync methods in Utf8BomInsertingStream to avoid the base Stream class's BeginEndReadAsync which schedules Read on a ThreadPool thread via Task.Factory.StartNew. Since the underlying Read is purely synchronous (wrapping a MemoryStream), the ThreadPool indirection is unnecessary and can cause rare timing-dependent NullReferenceException failures in the async deserialization path with small buffers. Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/f0425760-2006-4313-8df2-09c253d0a01f Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix test failure Read_NonGeneric_NoPublicConstructor_Throws
Fix intermittent NullReferenceException in Utf8BomInsertingStream by overriding ReadAsync
Apr 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
CollectionTestsDynamic_AsyncStreamWithSmallBuffer.Read_NonGeneric_NoPublicConstructor_Throwsintermittently throwsNullReferenceExceptioninstead of the expectedNotSupportedException(~2 hits/week in CI).The test-only
Utf8BomInsertingStreamclass doesn't overrideReadAsync, so the baseStreamimplementation routes throughBeginEndReadAsync→BeginReadInternal(serializeAsynchronously: true)→Task.Factory.StartNew, which schedules the synchronousReadon a ThreadPool thread. Since the underlying stream is aMemoryStreamwith no real I/O, this ThreadPool indirection is unnecessary and can cause rare timing-dependent failures in the async deserialization error path.ReadAsync(byte[], int, int, CancellationToken)to callReadsynchronously and return a completed taskReadAsync(Memory<byte>, CancellationToken)(under#if NET) to do the same, withMemoryMarshal.TryGetArrayfast path