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
- Open a Socket
- Call the
Memory<T>overload of the ReceiveAsync function - 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.
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>.
Source: mono/mono