Repository: microsoft/garnet
File: libs/common/Networking/TcpNetworkHandlerBase.cs (C#)

Code before:
        {
            e.AcceptSocket.Dispose();
            DisposeImpl();
            e.Dispose();
        }

        void RecvEventArg_Completed(object sender, SocketAsyncEventArgs e)
        {
            // Complete receive event and release thread while we process data async
            _ = HandleReceiveAsync(sender, e);
        }

        private async ValueTask HandleReceiveAsync(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                do
                {
                    if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success || serverHook.Disposed)
                    {
                        // No more things to receive
                        Dispose(e);
                        return;
                    }
                    var receiveTask = OnNetworkReceiveAsync(e.BytesTransferred);
                    if (!receiveTask.IsCompleted)
                    {
                        await receiveTask;
                    }
                    NetworkBufferProcessed(sender, e);
                }
                while (!e.AcceptSocket.ReceiveAsync(e));
            }
            catch (Exception ex)
            {
                HandleReceiveFailure(ex, e);
            }
        }

        void NetworkBufferProcessed(object sender, SocketAsyncEventArgs e)
        {
            e.SetBuffer(networkReceiveBuffer, networkBytesRead, networkReceiveBuffer.Length - networkBytesRead);
        }

        void HandleReceiveFailure(Exception ex, SocketAsyncEventArgs e)
        {
            if (ex is ObjectDisposedException ex2 && ex2.ObjectName == "System.Net.Sockets.Socket")
                logger?.LogTrace("Accept socket was disposed at RecvEventArg_Completed");
            else
                logger?.LogError(ex, "An error occurred at RecvEventArg_Completed");
            Dispose(e);

Review comment (suggestion):
`!IsCompletedSuccessfully` is better than `!IsCompleted` because it excludes faulted and canceled tasks, allowing the await to properly capture and rethrow those exceptions.

Code after:
        }

        void Dispose(SocketAsyncEventArgs e)
        {
            e.AcceptSocket.Dispose();
            DisposeImpl();
            e.Dispose();
        }

        void RecvEventArgCompletedWithTLS(object sender, SocketAsyncEventArgs e) =>
            _ = HandleReceiveWithTLSAsync(sender, e);

        void RecvEventArgCompletedWithoutTLS(object sender, SocketAsyncEventArgs e) =>
            HandleReceiveWithoutTLS(sender, e);

        private void HandleReceiveWithoutTLS(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                do
                {
                    if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success || serverHook.Disposed)
                    {
                        // No more things to receive
                        Dispose(e);
                        break;
                    }
                    OnNetworkReceiveWithoutTLS(e.BytesTransferred);
                    e.SetBuffer(networkReceiveBuffer, networkBytesRead, networkReceiveBuffer.Length - networkBytesRead);
                } while (!e.AcceptSocket.ReceiveAsync(e));
            }
            catch (Exception ex)
            {
                HandleReceiveFailure(ex, e);
            }
        }

        private async ValueTask HandleReceiveWithTLSAsync(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                do
                {
                    if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success || serverHook.Disposed)
                    {
                        // No more things to receive
                        Dispose(e);
                        break;
                    }
                    var receiveTask = OnNetworkReceiveWithTLSAsync(e.BytesTransferred);
                    if (!receiveTask.IsCompletedSuccessfully)