fix(s7): bound async wire ops with a wall-clock deadline (R2-01 read leg)
The R2-01 live gate (S7_1500ConnectTimeoutOutageTests, docker-pause blackhole) surfaced a real gap the offline fakes couldn't: S7.Net's ReadTimeout/WriteTimeout map to the TcpClient's ReceiveTimeout/SendTimeout, which govern only SYNCHRONOUS socket calls — the async read/write paths S7.Net uses ignore them. On an established-but-frozen peer (frozen PLC / firewall DROP / cable pulled mid-flow, TCP session still open) a read blocked until the OS TCP stack gave up (minutes), silently wedging the poll loop: no Bad tick, no reconnect. STAB-14 fixed only the CONNECT leg (EnsureConnectedAsync CancelAfter); this is its READ-leg sibling. - New S7OperationDeadline: bounds every data-plane wire op with a wall-clock ceiling (= _options.Timeout), surfacing an overrun as TimeoutException. Applied in S7PlcAdapter to Read/ReadBytes/Write/WriteBytes/ReadStatus. OpenAsync is left to EnsureConnectedAsync's own CancelAfter (not double-bounded). - IsS7ConnectionFatal now classifies TimeoutException fatal → handle marked dead → next EnsureConnectedAsync reopens (connect-timeout fix takes over from there). - Tests: 5 S7OperationDeadline unit tests (deadline / token-honouring / caller- cancel-passthrough / resultless), 1 driver-reaction test (read TimeoutException → reopen). Driver.S7.Tests 260/260. - Live gate S7_1500ConnectTimeoutOutageTests now GREEN against the real snap7 sim (8s: baseline Good -> pause blackhole -> Bad tick -> unpause -> recovered Good).
This commit is contained in:
@@ -87,6 +87,7 @@ public interface IS7PlcFactory
|
||||
internal sealed class S7PlcAdapter : IS7Plc
|
||||
{
|
||||
private readonly Plc _plc;
|
||||
private readonly TimeSpan _operationTimeout;
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="S7PlcAdapter"/> class.</summary>
|
||||
/// <param name="cpuType">The CPU family.</param>
|
||||
@@ -98,9 +99,14 @@ internal sealed class S7PlcAdapter : IS7Plc
|
||||
public S7PlcAdapter(S7CpuType cpuType, string host, int port, short rack, short slot, TimeSpan timeout)
|
||||
{
|
||||
_plc = new Plc(S7CpuTypeMap.ToS7Net(cpuType), host, port, rack, slot);
|
||||
_operationTimeout = timeout;
|
||||
// S7netplus writes timeouts into the underlying TcpClient via ReadTimeout / WriteTimeout
|
||||
// (milliseconds). Set before OpenAsync so the handshake itself honours the bound — matches
|
||||
// the original inline construction in S7Driver.InitializeAsync.
|
||||
// the original inline construction in S7Driver.InitializeAsync. NOTE: these map to the
|
||||
// TcpClient's ReceiveTimeout / SendTimeout, which govern only SYNCHRONOUS socket calls; the
|
||||
// async read/write paths S7.Net actually uses ignore them, so every data-plane op below is
|
||||
// additionally bounded by S7OperationDeadline (a real wall-clock ceiling). OpenAsync is left
|
||||
// to S7Driver.EnsureConnectedAsync's own CancelAfter, so it is not double-bounded here.
|
||||
var ms = (int)timeout.TotalMilliseconds;
|
||||
_plc.ReadTimeout = ms;
|
||||
_plc.WriteTimeout = ms;
|
||||
@@ -117,23 +123,30 @@ internal sealed class S7PlcAdapter : IS7Plc
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<object?> ReadAsync(string address, CancellationToken cancellationToken) =>
|
||||
_plc.ReadAsync(address, cancellationToken)!;
|
||||
S7OperationDeadline.RunAsync(
|
||||
t => _plc.ReadAsync(address, t)!, _operationTimeout, $"read of '{address}'", cancellationToken);
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<byte[]> ReadBytesAsync(S7NetDataType area, int db, int startByteAdr, int count, CancellationToken cancellationToken) =>
|
||||
_plc.ReadBytesAsync(area, db, startByteAdr, count, cancellationToken);
|
||||
S7OperationDeadline.RunAsync(
|
||||
t => _plc.ReadBytesAsync(area, db, startByteAdr, count, t),
|
||||
_operationTimeout, $"block read of {area} db{db} @{startByteAdr}+{count}", cancellationToken);
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task WriteAsync(string address, object value, CancellationToken cancellationToken) =>
|
||||
_plc.WriteAsync(address, value, cancellationToken);
|
||||
S7OperationDeadline.RunAsync(
|
||||
t => _plc.WriteAsync(address, value, t), _operationTimeout, $"write of '{address}'", cancellationToken);
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task WriteBytesAsync(S7NetDataType area, int db, int startByteAdr, byte[] value, CancellationToken cancellationToken) =>
|
||||
_plc.WriteBytesAsync(area, db, startByteAdr, value, cancellationToken);
|
||||
S7OperationDeadline.RunAsync(
|
||||
t => _plc.WriteBytesAsync(area, db, startByteAdr, value, t),
|
||||
_operationTimeout, $"block write of {area} db{db} @{startByteAdr}", cancellationToken);
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task ReadStatusAsync(CancellationToken cancellationToken) =>
|
||||
_ = await _plc.ReadStatusAsync(cancellationToken).ConfigureAwait(false);
|
||||
public Task ReadStatusAsync(CancellationToken cancellationToken) =>
|
||||
S7OperationDeadline.RunAsync(
|
||||
t => _plc.ReadStatusAsync(t), _operationTimeout, "status read", cancellationToken);
|
||||
|
||||
/// <inheritdoc />
|
||||
// Plc implements IDisposable explicitly, so cast to invoke it; disposal tears down the TcpClient.
|
||||
|
||||
Reference in New Issue
Block a user