Skip to content

StreamPipeReader NullReferenceException in some multithreading scenarios #101893

Open
@JamesNK

Description

@JamesNK

Description

NullReferenceException has been observed from StreamPipeReader. Likely caused by misuse (multiple concurrent calls to ReadAsync), however a more useful error should be thrown than NRE.


From investigation:

There is a nullref if the internal Stream.ReadAsync call returns and StreamPipeReader is adding a new BufferSegment while at the same time the reader is being completed and unsets BufferSegment state.

The specific lines are BufferSegment.cs where segment.NextSegment can be null if BufferSegment.cs sets _next to null right after the while check in SetNext.

Another bug in the same scenario is an ArgumentOutOfRange exception when incrementing the End value at StreamPipeReader.cs which calls Slice on the AvailableMemory which is now empty from the Complete call.

Reproduction Steps

Possibly caused by multiple concurrent ReadAsync calls.

Expected behavior

More informative error. Or no error. Should look at what other pipe readers do in this situation.

Actual behavior

NullReferenceException thrown.

Regression?

No

Known Workarounds

No response

Configuration

.NET 8

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions