Skip to content

fix(s7): bound every PLC request with a deadline - #246

Merged
mrcsin merged 1 commit into
masterfrom
transport-deadline
Nov 30, 2026
Merged

mrcsin merged 1 commit into
masterfrom
transport-deadline

Conversation

@mrcsin

@mrcsin mrcsin commented Nov 30, 2026

Copy link
Copy Markdown
Member

Problem

No PLC request had a time limit. A PLC that accepts the connection and then stops answering left a connect, read or write hanging forever: the transport gate stayed held, the keep-alive probe queued behind it, so link loss was never declared and the link slot kept its last state. Any flow waiting on that request could not finish. S7Driver.ConnectAsync also replaced its client without closing the old one, so repeated reconnects could use up PLC connection slots.

What changed

  • Probed S7netplus 0.20.0 against a listener that never answers: ReadTimeout and WriteTimeout do nothing for async calls; a cancelled token ends connect, read and write about 20 ms later, and the library then closes the client. The plan's Caveats record it.
  • S7Driver gives every request its own 5 s deadline (TransportDeadline). Block reads and writes are split into chunks of the negotiated PDU size, the library's own split sizes, and each chunk is one call with its own deadline, so a large recipe on a slow link does not time out as one call.
  • Passing the deadline closes the client and declares link loss through the same path as the keep-alive probe, after the transport gate is released. The reconnect loop then runs. A write cut off this way is a failed write.
  • ConnectAsync closes the old client first. A negotiated PDU too small to hold a chunk throws instead of looping.
  • The unused writing_timeout_ms setting is removed from the settings, the shipped configs and the docs. The YAML loader ignores unknown keys, so older configs still load.

This is Task 3a of docs/plans/20261130-recipe-replace-save-exit-safety.md. A timed-out version read on reconnect still turns sync off; Task 3b changes that.

Verification

  • Build 0 warnings; full suite 2223 passed, 0 failed; dotnet format --verify-no-changes clean.
  • New tests: connect, read and write against a request that never answers each end within the deadline, declare link loss and release the gate; a caller cancel declares nothing; every chunk of a slow multi-chunk transfer gets its own deadline and the bytes reassemble exactly; an exact multiple sends no empty request; a too-small PDU throws.
  • Live against the PLC emulator: connect, write, edit, run, emulator closed ("нет связи" in 0.4 s), restart and reconnect; a suspended emulator trips the 5 s deadline and recovers on resume. A 264-step recipe (float, int and string blocks each over one PDU) writes byte for byte, and the reconnect comparison reads it back as identical.
  • Code review and two independent reviews; their findings are fixed in this PR.

Refs #181

🤖 Generated with Claude Code

A PLC that accepts the TCP connection and stops answering hung the app
without a limit. S7Driver passed no timeout to S7netplus, and the
library's ReadTimeout and WriteTimeout do not apply to its async calls,
so one stuck read or write held the transport gate. The keep-alive
probe queued behind that gate, so link loss was never declared and the
reconnect loop never ran. A stuck connect also blocked the reconnect
loop and DisconnectAsync, and each ConnectAsync left the previous
client open.

Each connect attempt and each PDU request now runs on a token that
S7Driver cancels after S7Driver.TransportDeadline (5 s). A probe showed
that S7netplus 0.20.0 ends connect, read and write on that token.
S7netplus splits a block into several PDU requests on one token, so a
large array on a slow PLC would pass one shared deadline on a healthy
link. The driver therefore sends each block in chunks of one PDU, sized
from the negotiated Plc.MaxPDUSize minus the library's request
overhead, and each chunk gets its own deadline. Order and content of
the bytes on the wire do not change.

On the deadline the driver closes the client and throws
TimeoutException. The executor reports that exception as
PlcCommandFailedError, so a cut-off recipe write is a failed write.
Once the gate is released, the driver raises RequestDeadlinePassed, and
S7Service calls OnConnectionLost on the thread pool: the poll that
passed the deadline must not wait for itself to stop. ConnectAsync
closes the old client before it creates a new one.

writing_timeout_ms had no reader and now overlaps the deadline, so it
is removed from the settings, the shipped configs and the docs. The
YAML loader ignores unknown keys, so old configs still load.

Refs #181

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mrcsin
mrcsin marked this pull request as ready for review November 30, 2026 13:10
@mrcsin
mrcsin merged commit 754df50 into master Nov 30, 2026
1 check passed
@mrcsin
mrcsin deleted the transport-deadline branch November 30, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant