fix(notify,esg): classify dropped SMTP connections, OAuth2 token-endpoint outages and body-read connection loss as transient (NTF-2, ESG-1, WP4 task 4.6)

This commit is contained in:
Joseph Doherty
2026-12-03 09:58:10 -05:00
parent 5f7b850af2
commit 11992075e8
8 changed files with 210 additions and 14 deletions
@@ -396,6 +396,22 @@ public class ExternalSystemClient : IExternalSystemClient
throw ErrorClassifier.AsTransient(
$"Timeout reading response from {system.Name} after {effectiveTimeout.TotalSeconds:0.##}s", ex);
}
catch (Exception ex) when (ex is IOException or HttpRequestException)
{
// ESG-1: headers arrived, then the connection was reset or the body truncated
// (IOException / HttpIOException / HttpRequestException from the stream read).
// Nothing classified it, so a raw IOException escaped into the script and a
// CachedCall buffered nothing. A success or transient status is a transient
// failure — the call never completed. A permanent status stays permanent:
// losing its error body does not make it retryable.
if (response.IsSuccessStatusCode || ErrorClassifier.IsTransient(response.StatusCode))
{
throw ErrorClassifier.AsTransient(
$"Connection lost reading the response from {system.Name}: {ex.Message}", ex);
}
body = $"(response body unreadable: {ex.Message})";
}
if (response.IsSuccessStatusCode)
{
@@ -199,9 +199,11 @@ public sealed class EmailNotificationDeliveryAdapter : INotificationDeliveryAdap
}
catch (Exception ex)
{
// An unclassified failure — chiefly an OAuth2 token-fetch failure. The
// outbox treats it as permanent: retrying a broken credential burns
// token-endpoint calls. (Mirrors the default-to-permanent stance.)
// An unclassified failure — chiefly a broken OAuth2 credential (a malformed
// credential triple, a 4xx from the token endpoint). The outbox treats it as
// permanent: retrying a broken credential burns token-endpoint calls. A token
// endpoint outage (5xx/408/429, no response, timeout) and a dropped SMTP
// connection are classified transient above (NTF-2) and never reach here.
var detail = CredentialRedactor.Scrub(ex.Message, smtpConfig.Credentials);
_logger.LogError(
"Unclassified failure delivering email to list '{List}' ({ExceptionType}): {Detail}",
@@ -1,6 +1,7 @@
using System.Net.Sockets;
using MailKit;
using MailKit.Net.Smtp;
using MailKit.Security;
namespace ZB.MOM.WW.ScadaBridge.NotificationService;
@@ -72,16 +73,44 @@ public static class SmtpErrorClassifier
return SmtpErrorClass.Unknown;
}
// Protocol errors, a dropped/unavailable service, socket failures and
// timeouts are all retryable — the message has not been rejected.
if (ex is SmtpProtocolException
or ServiceNotConnectedException
or SocketException
or TimeoutException)
// OAuth2 token endpoint (NTF-2): classify the HTTP failure by status. No status means
// the request never got an answer (DNS, connect, reset) — transient; a 5xx/408/429 is
// an outage or throttle — transient; any other 4xx is a broken credential or request
// and stays permanent so a bad secret does not burn token-endpoint calls on retry.
if (ex is HttpRequestException http)
{
return http.StatusCode is not { } status
|| (int)status >= 500
|| (int)status is 408 or 429
? SmtpErrorClass.Transient
: SmtpErrorClass.Permanent;
}
// A cancellation the caller did NOT request is a timeout (HttpClient.Timeout on the
// token fetch surfaces as TaskCanceledException) — transient (NTF-2).
if (ex is OperationCanceledException)
{
return SmtpErrorClass.Transient;
}
// Protocol errors, a dropped/unavailable service, socket failures, timeouts, a dropped
// connection (IOException — MailKit's documented connection-loss exception on
// Connect/Authenticate/Send) and a TLS handshake that failed mid-way are all retryable:
// the message has not been rejected (NTF-2). The inner-exception chain is walked
// because MailKit typically wraps the SocketException in an IOException.
for (var e = ex; e is not null; e = e.InnerException)
{
if (e is SmtpProtocolException
or ServiceNotConnectedException
or SocketException
or TimeoutException
or SslHandshakeException
or IOException)
{
return SmtpErrorClass.Transient;
}
}
return SmtpErrorClass.Unknown;
}