From ad07c549b3807f70d42c3581af031b4176546a28 Mon Sep 17 00:00:00 2001 From: DJGosnell Date: Tue, 12 Sep 2023 21:32:57 -0400 Subject: [PATCH] Fix for await calls. Additional exception catching on session closure. --- src/NexNet/Internals/NexusSession.cs | 20 ++++++++++++++++++-- src/NexNet/Pipes/NexusChannelExtensions.cs | 15 ++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/NexNet/Internals/NexusSession.cs b/src/NexNet/Internals/NexusSession.cs index 0c599b4..5a3e227 100644 --- a/src/NexNet/Internals/NexusSession.cs +++ b/src/NexNet/Internals/NexusSession.cs @@ -279,7 +279,15 @@ private async ValueTask DisconnectCore(DisconnectReason reason, bool sendDisconn if (_config.InternalNoLingerOnShutdown) { - await _transportConnection.CloseAsync(false).ConfigureAwait(false); + try + { + await _transportConnection.CloseAsync(false).ConfigureAwait(false); + } + catch (Exception e) + { + Logger?.LogError(e, "Error while closing transport connection."); + } + return; } else @@ -302,7 +310,15 @@ private async ValueTask DisconnectCore(DisconnectReason reason, bool sendDisconn } _pipeOutput = null; - await _transportConnection.CloseAsync(true); + try + { + await _transportConnection.CloseAsync(true).ConfigureAwait(false); + } + catch (Exception e) + { + Logger?.LogError(e, "Error while closing transport connection."); + } + } // If we match a limited type of disconnects, attempt to reconnect if we are the client diff --git a/src/NexNet/Pipes/NexusChannelExtensions.cs b/src/NexNet/Pipes/NexusChannelExtensions.cs index 5113961..2eea77d 100644 --- a/src/NexNet/Pipes/NexusChannelExtensions.cs +++ b/src/NexNet/Pipes/NexusChannelExtensions.cs @@ -31,7 +31,7 @@ public static async ValueTask WriteAndComplete( CancellationToken cancellationToken = default) { var writer = await channel.GetWriterAsync(); - await WriteAndComplete(writer, enumerableData, chunkSize, cancellationToken); + await WriteAndComplete(writer, enumerableData, chunkSize, cancellationToken).ConfigureAwait(false); } /// @@ -58,19 +58,19 @@ public static async ValueTask WriteAndComplete( foreach (var chunk in dataChunks) { - await writer.WriteAsync(chunk, cancellationToken); + await writer.WriteAsync(chunk, cancellationToken).ConfigureAwait(false); } } else { foreach (var data in enumerableData) { - await writer.WriteAsync(data, cancellationToken); + await writer.WriteAsync(data, cancellationToken).ConfigureAwait(false); } } - await writer.CompleteAsync(); + await writer.CompleteAsync().ConfigureAwait(false); } /// @@ -107,8 +107,8 @@ public static async ValueTask> ReadUntilComplete( int estimatedSize = 0, CancellationToken cancellationToken = default) { - var reader = await channel.GetReaderAsync(); - return await ReadUntilComplete(reader, estimatedSize, cancellationToken); + var reader = await channel.GetReaderAsync().ConfigureAwait(false); + return await ReadUntilComplete(reader, estimatedSize, cancellationToken).ConfigureAwait(false); } /// @@ -153,7 +153,8 @@ public static async ValueTask> ReadUntilComplete( if (reader.IsComplete && previousBufferLength == 0) break; - var readResult = await reader.ReadAsync(converter, cancellationToken); + var readResult = await reader.ReadAsync(converter, cancellationToken) + .ConfigureAwait(false); var resultCount = readResult.Count; if (reader.IsComplete && resultCount == 0