Skip to content

Fix intermittent NullReferenceException in Utf8BomInsertingStream by overriding ReadAsync#127453

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-test-failure-non-generic-constructor
Draft

Fix intermittent NullReferenceException in Utf8BomInsertingStream by overriding ReadAsync#127453
Copilot wants to merge 2 commits intomainfrom
copilot/fix-test-failure-non-generic-constructor

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 27, 2026

Description

CollectionTestsDynamic_AsyncStreamWithSmallBuffer.Read_NonGeneric_NoPublicConstructor_Throws intermittently throws NullReferenceException instead of the expected NotSupportedException (~2 hits/week in CI).

The test-only Utf8BomInsertingStream class doesn't override ReadAsync, so the base Stream implementation routes through BeginEndReadAsyncBeginReadInternal(serializeAsynchronously: true)Task.Factory.StartNew, which schedules the synchronous Read on a ThreadPool thread. Since the underlying stream is a MemoryStream with no real I/O, this ThreadPool indirection is unnecessary and can cause rare timing-dependent failures in the async deserialization error path.

  • Override ReadAsync(byte[], int, int, CancellationToken) to call Read synchronously and return a completed task
  • Override ReadAsync(Memory<byte>, CancellationToken) (under #if NET) to do the same, with MemoryMarshal.TryGetArray fast path
  • Eliminates the ThreadPool hop entirely for this test wrapper stream

Copilot AI self-assigned this Apr 27, 2026
Copilot AI review requested due to automatic review settings April 27, 2026 13:53
Copilot AI review requested due to automatic review settings April 27, 2026 13:53
Copilot AI linked an issue Apr 27, 2026 that may be closed by this pull request
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

…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 requested review from Copilot and removed request for Copilot April 27, 2026 14:46
Copilot AI changed the title [WIP] Fix test failure Read_NonGeneric_NoPublicConstructor_Throws Fix intermittent NullReferenceException in Utf8BomInsertingStream by overriding ReadAsync Apr 27, 2026
Copilot AI requested a review from eiriktsarpalis April 27, 2026 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test Failure: Read_NonGeneric_NoPublicConstructor_Throws

2 participants