fix(opcuaclient): #676 live gate A1 — a sustained closed connection is a definitive loss

Live gate A1 (docker-dev, opc-plc restart / SIGINT / kill): the fast path never fired —
a real process stop delivers no session status; the keep-alives report
BadSecureChannelClosed (0x80860000) once, then BadConnectionClosed (0x80AE0000)
interleaved with BadNoCommunication for the whole outage.

Reproduced in-process with TcpStallProxy.HardClose (reset every connection, refuse new
ones; server and session alive behind it): the identical sequence, and the SDK 1.5.378
session never opens a new connection on its own (48 s with the proxy accepting again,
zero attempts) — a closed channel cannot recover in place.

The driver now treats BadSecureChannelClosed/BadConnectionClosed as definitive on the
SECOND closed-channel keep-alive failure in one outage (a good keep-alive resets the
count), raising ConnectionLost ~1 keep-alive after the drop. BadNoCommunication /
BadRequestTimeout / BadNotConnected (stall/pause) stay with the watchdog; the stall test
still sees no ConnectionLost. New wire test (hard close + reopen ⇒ raised once, SDK never
reconnects, reinit brings the identical value back Good) and unit tests (sustained raises
once; a single blip that recovers does not) — all red on the previous code. Docs updated.
This commit is contained in:
Joseph Doherty
2026-10-11 12:34:52 -04:00
parent 945ec2c069
commit beb1c583b1
7 changed files with 218 additions and 17 deletions
@@ -19,6 +19,7 @@ internal sealed class TcpStallProxy : IAsyncDisposable
private readonly List<TcpClient> _sockets = [];
private readonly Lock _lock = new();
private TaskCompletionSource _gate = Open();
private volatile bool _refusing;
private Task _acceptLoop = Task.CompletedTask;
private TcpStallProxy(int targetPort) => _targetPort = targetPort;
@@ -48,6 +49,34 @@ internal sealed class TcpStallProxy : IAsyncDisposable
if (_gate.Task.IsCompleted) _gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
}
/// <summary>
/// The shape of a killed / stopped server process (#676 live gate): every open connection is RESET (linger 0 ⇒
/// RST, both sides) and new connections are refused (accepted and reset at once) until <see cref="Reopen"/>.
/// </summary>
public void HardClose()
{
List<TcpClient> victims;
lock (_lock)
{
_refusing = true;
victims = [.. _sockets];
_sockets.Clear();
}
foreach (var s in victims) Reset(s);
}
/// <summary>Accept and forward new connections again (the server is back); the reset ones stay dead.</summary>
public void Reopen() => _refusing = false;
/// <summary>Connections refused while <see cref="HardClose"/> was in force — a reconnect ATTEMPT by the client.</summary>
public int RefusedConnections { get; private set; }
private static void Reset(TcpClient s)
{
try { s.Client.LingerState = new LingerOption(true, 0); } catch { }
try { s.Dispose(); } catch { }
}
/// <summary>Releases everything held since <see cref="Pause"/>, in order.</summary>
public void Resume()
{
@@ -73,6 +102,12 @@ internal sealed class TcpStallProxy : IAsyncDisposable
TcpClient client;
try { client = await _listener.AcceptTcpClientAsync(_cts.Token); }
catch { return; }
if (_refusing)
{
lock (_lock) RefusedConnections++;
Reset(client);
continue;
}
var upstream = new TcpClient();
try { await upstream.ConnectAsync(IPAddress.Loopback, _targetPort, _cts.Token); }
catch