Skip to content

Commit

Permalink
Fix for await calls.
Browse files Browse the repository at this point in the history
Additional exception catching on session closure.
  • Loading branch information
DJGosnell committed Sep 13, 2023
1 parent 6f19beb commit ad07c54
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
20 changes: 18 additions & 2 deletions src/NexNet/Internals/NexusSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 8 additions & 7 deletions src/NexNet/Pipes/NexusChannelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static async ValueTask WriteAndComplete<T>(
CancellationToken cancellationToken = default)
{
var writer = await channel.GetWriterAsync();
await WriteAndComplete<T>(writer, enumerableData, chunkSize, cancellationToken);
await WriteAndComplete<T>(writer, enumerableData, chunkSize, cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -58,19 +58,19 @@ public static async ValueTask WriteAndComplete<T>(

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);
}

/// <summary>
Expand Down Expand Up @@ -107,8 +107,8 @@ public static async ValueTask<List<T>> ReadUntilComplete<T, TTo>(
int estimatedSize = 0,
CancellationToken cancellationToken = default)
{
var reader = await channel.GetReaderAsync();
return await ReadUntilComplete<T>(reader, estimatedSize, cancellationToken);
var reader = await channel.GetReaderAsync().ConfigureAwait(false);
return await ReadUntilComplete<T>(reader, estimatedSize, cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -153,7 +153,8 @@ public static async ValueTask<List<TTo>> ReadUntilComplete<T, TTo>(
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
Expand Down

0 comments on commit ad07c54

Please sign in to comment.