Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle various protocol errors and socket exceptions #584

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/NATS.Client.Core/Commands/CommandWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Buffers;
using System.IO.Pipelines;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading.Channels;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -593,6 +594,21 @@ private static async Task ReaderLoopAsync(
{
// Expected during shutdown
}
catch (SocketException e)
{
logger.LogWarning(NatsLogEvents.Buffer, e, "Socket error in send buffer reader loop");
try
{
// We signal the connection to disconnect, which will trigger a reconnect
// in the connection loop. This is necessary because the connection may
// be half-open, and we can't rely on the reader loop to detect that.
connection.SignalDisconnected(e);
}
catch (Exception e1)
{
logger.LogWarning(NatsLogEvents.Buffer, e1, "Error when signaling disconnect");
}
}
catch (Exception e)
{
logger.LogError(NatsLogEvents.Buffer, e, "Unexpected error in send buffer reader loop");
Expand Down
5 changes: 3 additions & 2 deletions src/NATS.Client.Core/NatsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,9 @@ private async ValueTask SetupReaderWriterAsync(bool reconnect)

try
{
// wait for INFO
await waitForInfoSignal.Task.ConfigureAwait(false);
// Wait for an INFO message from server. If we land on a dead socket and server response
// can't be received, this will throw a timeout exception and we will retry the connection.
await waitForInfoSignal.Task.WaitAsync(Opts.RequestTimeout).ConfigureAwait(false);

// check to see if we should upgrade to TLS
if (_socket is TcpConnection tcpConnection)
Expand Down
2 changes: 2 additions & 0 deletions src/NATS.Client.JetStream/Internal/NatsJSConsume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ protected override async ValueTask ReceiveInternalAsync(
{
_notificationChannel?.Notify(new NatsJSProtocolNotification("Unhandled protocol message", headers.Code, headers.MessageText));
_logger.LogWarning(NatsJSLogEvents.ProtocolMessage, "Unhandled protocol message: {Code} {Description}", headers.Code, headers.MessageText);
_userMsgs.Writer.TryComplete(new NatsJSProtocolException(headers.Code, headers.Message, headers.MessageText));
EndSubscription(NatsSubEndReason.JetStreamError);
}
}
else
Expand Down
2 changes: 2 additions & 0 deletions src/NATS.Client.JetStream/Internal/NatsJSFetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ protected override async ValueTask ReceiveInternalAsync(
{
_notificationChannel?.Notify(new NatsJSProtocolNotification("Unhandled protocol message", headers.Code, headers.MessageText));
_logger.LogWarning(NatsJSLogEvents.ProtocolMessage, "Unhandled protocol message: {Code} {Description}", headers.Code, headers.MessageText);
_userMsgs.Writer.TryComplete(new NatsJSProtocolException(headers.Code, headers.Message, headers.MessageText));
EndSubscription(NatsSubEndReason.JetStreamError);
}
}
else
Expand Down
2 changes: 2 additions & 0 deletions src/NATS.Client.JetStream/Internal/NatsJSOrderedConsume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ protected override async ValueTask ReceiveInternalAsync(
else
{
_logger.LogWarning(NatsJSLogEvents.ProtocolMessage, "Unhandled protocol message: {Code} {Description}", headers.Code, headers.MessageText);
_userMsgs.Writer.TryComplete(new NatsJSProtocolException(headers.Code, headers.Message, headers.MessageText));
EndSubscription(NatsSubEndReason.JetStreamError);
}
}
else
Expand Down
Loading