Skip to content

fix(plc): pause writes until the comparison decides - #247

Merged
mrcsin merged 1 commit into
masterfrom
write-pause
Nov 30, 2026
Merged

mrcsin merged 1 commit into
masterfrom
write-pause

Conversation

@mrcsin

@mrcsin mrcsin commented Nov 30, 2026

Copy link
Copy Markdown
Member

Problem

With sync on, the app could write the local recipe to the PLC before the reconnect comparison read the PLC recipe (#181): EnableSync turned sync on before it connected, so an edit during the connect, or a link that came back inside the 1 s debounce, wrote first and the conflict was never seen. A failed read during the comparison also wrote the local recipe. Closing the conflict dialog without a choice left sync on, so the next edit overwrote the PLC recipe. A write could slip out between the app finding a conflict and the operator answering it. A version check that failed on reconnect only logged. The sync switch read its direction when the command ran, not at the press.

What changed

  • A write pause starts when sync turns on and when the link drops, and lasts until the reconnect comparison decides: identical, PLC empty or uncommitted recipe write the screen recipe; a conflict holds the pause until it is answered; the existing take of a PLC recipe onto an empty screen ends it without a write. PlcSyncCoordinator owns the pause.
  • Each comparison has an ID; a link drop, a new comparison or sync off makes older ones stale, and stale results, failed reads, conflicts and answers are ignored.
  • A failed or timed-out comparison read declares link loss and the reconnect loop compares again; three failed comparisons in a row turn sync off. A version mismatch on reconnect turns sync off; the first-connect comparison runs after the enable's version read and skips its own.
  • Closing the conflict dialog with X, Esc or Alt+F4, a dialog that cannot be shown, and a PLC recipe that fails the check turn sync off. The notification panel keeps the reason until sync is turned on again. PlcConflictDialog handles Esc.
  • Turning sync on and off runs one at a time; the switch keeps the direction it showed at the press; app turn-offs are posted outside the comparison and re-validated when they run.
  • The reconnect take checks again on the UI thread that the screen recipe is empty and unsaved, and raises the conflict otherwise.
  • PlcLifecycleManager is split into PlcSyncSwitch, PlcComparisonState, PlcReconnectComparison, PlcComparisonAnswers, PlcConflictNotifier and PlcReconnectTake.
  • Docs: docs/readme.md (sync, conflict, notifications), dialogs-and-interactions.md, error-reporting.md, exit-flow.md, and the plan's Behavior line on the notification.

This is Task 3b of docs/plans/20261130-recipe-replace-save-exit-safety.md.

Verification

  • Build 0 warnings; full suite 2277 passed, 0 failed; dotnet format --verify-no-changes clean.
  • Tests cover rows D1, D3-D12, A1, A7, A8, A9: an edit during the connect and a reconnect inside the debounce write nothing before the comparison; stale IDs; the three-failure cap; posted turn-off races; throwing handlers and takes; Esc on the dialog; the switch direction; the persistent message.
  • Live against the PLC emulator: with the conflict dialog open the PLC kept its value for 8 s; "Оставить локальный" wrote it 1 s later; Esc turned sync off and the message survived an edit; "Загрузить из ПЛК" brought back the PLC values; an edit while the emulator was down was written only after the reconnect comparison.
  • Code review and two independent reviews; their findings are fixed in this PR.

Closes #181

🤖 Generated with Claude Code

Sync wrote the screen recipe to the PLC before the reconnect comparison
read the PLC recipe (#181). An edit after a link drop started the 1 s
debounce, and a reconnect inside it wrote that edit over a recipe the
comparison had not seen yet. The same happened to an edit made while
sync on was still connecting. A failed comparison read also wrote the
screen recipe, a conflict dialog closed without a choice left sync on
for the next edit to overwrite the PLC, an answer from before a link
drop still acted, and a version mismatch on reconnect disconnected
from inside the comparison, waiting on the reconnect loop, and turned
sync off without a message.

PlcSyncCoordinator now holds a write pause. Sync on and a link loss
start it and drop a debounced write; a write already in flight ends as
before. Edits during the pause stay on screen and are kept, and no
debounce, run end or finished write starts a new write while it holds.
PlcLifecycleManager gives each comparison an ID, which a link drop, a
newer comparison or sync off makes stale. Each current outcome ends the
pause in one step with the ID check: identical, PLC empty or not
committed end it with a write of the newest screen recipe, a conflict
keeps it, a take ends it without a write. Stale results and answers
change nothing.

A failed, deadline-passed or throwing comparison read closes the client
and declares link loss through IS7Connection.DeclareLinkLost, so the
reconnect loop compares again. A comparison cut by a link drop counts
as failed too, so the count does not depend on whether the drop or the
failed read arrives first. Three in a row turn sync off. A version
mismatch on reconnect turns sync off with the version message. The
first-connect comparison runs after EnableSync's version read and
skips its own, so a mismatched PLC is never compared or written.

The conflict answers carry the comparison ID. Closing the dialog
without a choice (X, Esc, Alt+F4; Esc gets a handler) or a dialog that
cannot be shown turns sync off, and so does a PLC recipe that fails
the take check, in the dialog or on the reconnect take. Sync on and
off run one at a time behind one gate, and a comparison starts only
while the sync-on flag, kept under the comparison lock, is set. The app
posts its turn-offs so the comparison and the reconnect loop never wait
on them; each is checked again when it runs and does nothing after sync
off, in a newer sync-on period, or for the failure cap once a later
comparison decided. The switch acts in the direction it showed at the
press, so a press during an app turn-off cannot turn sync back on.

A conflict handler that throws, or a take that throws, also ends the
conflict with sync off instead of holding the pause with no dialog.
The reconnect take checks again on the UI thread that the screen
recipe is empty and saved; an Open that landed during the read gets
the conflict instead. Each turn-off reason reaches the notification
panel as a typed error on PlcLifecycleManager.SyncTurnOffs and stays
there through edits until sync is turned on again.

PlcLifecycleManager is now a facade over PlcSyncSwitch,
PlcComparisonState, PlcReconnectComparison, PlcConflictNotifier,
PlcReconnectTake and PlcComparisonAnswers. S7Service.DeclareLinkLost
marks the link lost before it closes the client, so the close cannot
hit a link the reconnect loop opened.

Closes #181

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mrcsin
mrcsin marked this pull request as ready for review November 30, 2026 15:22
@mrcsin
mrcsin merged commit cd0b332 into master Nov 30, 2026
1 check passed
@mrcsin
mrcsin deleted the write-pause branch November 30, 2026 15:28
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.

Auto-push can overwrite the PLC recipe before reconciliation reads it

1 participant