#20902·mono

SocketTaskExtensions.ReceiveAsync(Memory<T>) always reads zeroes.

Author: jduncanatorCreated Mar 6, 2021Updated May 4, 2026

The Memory<T> overload of the SocketTaskExtensions.ReceiveAsync extension method always reads zeroes into the Memory<T>. It should read the bytes read from the network into the Memory<T>.

Steps to Reproduce

  1. Open a Socket
  2. Call the Memory<T> overload of the ReceiveAsync function
  3. Observe it returning the correct amount of bytes read, but reading all zeroes into the Memory<T>.

Taking a look at the Mono implementation of this overload makes the issue pretty obvious.

https://github.com/mono/mono/blob/da11592cbea4269971f4b1f9624769a85cc10660/mcs/class/System/System.Net.Sockets/SocketTaskExtensions.cs#L254-L267

The current implementation creates a copy of the Memory<T> by way of ToArray(), but doesn't store a reference to it. The BeginReceive is essentially reading into a temporary array that is thrown away at the end of the function call, so the result never ends up in the Memory<T>.