fix(opcuaclient): #926 review r1 - loss after the signal withdraws known; gaps outside a bracket, overlapping starts, failed refreshes

- a sequence gap outside a bracket is a loss (re-refresh, same budget); older/repeated
  sequence numbers are ignored and not recorded
- AlarmInitialStateEventArgs.Withdrawn (additive): the driver withdraws 'known' once per
  signalled episode on its first loss; DriverHostActor sends MarkConditionsKnown(Known:false)
  for those conditions, drops them from the backstop and re-arms F5
- a RefreshStart while a bracket is open is a loss (never erases the open bracket's)
- a failed ConditionRefresh call is retried through the same bounded retry
- retry generation token (a finished retry never clears a newer retry's pending flag)
- spent budget renews after a 5-min cool-down; RefreshRequired sweeps coalesced
Red/green per fix (7 mutations) + live opc-plc gate re-run.
This commit is contained in:
Joseph Doherty
2026-11-28 10:07:08 -05:00
parent 99f2b9d8b7
commit 06da40a909
12 changed files with 534 additions and 75 deletions
+4 -2
View File
@@ -483,8 +483,10 @@ refresh, EventIds) is documented in [Alarm tracking § Imported conditions](Alar
unknown).
- An OpcUaClient RefreshEnd signals only for a lossless refresh bracket (#926). Loss inside the bracket — queue
overflow, RefreshRequired, a skipped publish gap, or a missing RefreshStart — suppresses the signal and re-runs
ConditionRefresh, at most 3 times. If every retry is lossy, the conditions stay unknown and the timeout diagnostic
reports them. See `docs/drivers/OpcUaClient.md`.
ConditionRefresh, at most 3 times per episode. If every retry is lossy, the conditions stay unknown and the timeout
diagnostic reports them. A loss after the signal **withdraws** "known" for that subscription's conditions
(`AlarmInitialStateEventArgs.Withdrawn` ⇒ `MarkConditionsKnown(Known: false)`, F5 re-armed, no backstop) until a
clean refresh signals again. See `docs/drivers/OpcUaClient.md`.
### Condition key (`DataConnections:ConditionKeySecret`)
+11 -4
View File
@@ -463,10 +463,17 @@ initialise (the AdminUI form drops them on the next save). Pinned by
> - a delivered publish message skipped a sequence number. This catches the gap the SDK gives up on after a failed
> republish, which the SDK reports nowhere else.
>
> A lossy bracket logs a Warning and re-runs ConditionRefresh on that subscription. At most 3 re-refreshes run per
> episode, backing off 2 s, 4 s and 8 s, and a clean RefreshEnd resets the budget. When it is spent, nothing signals:
> the conditions stay unknown (`Uncertain`) and the host's `ConditionKnownSignalTimeout` diagnostic reports them.
> An overflow outside a bracket (lost live transitions) also requests a re-refresh. The alarm event item asks for a
> A RefreshStart that arrives while a bracket is still open is a loss too.
>
> Any loss logs a Warning and re-runs ConditionRefresh on that subscription. That includes an overflow or sequence gap
> outside a bracket, which can hold a lost live transition, and a failed ConditionRefresh call.
>
> - **Withdraw on a later loss:** a loss after the subscription signalled also **withdraws "known"**
> (`AlarmInitialStateEventArgs.Withdrawn`). The host takes known back for those conditions (`Uncertain` on a
> consumer), keeps them out of the backstop, and diagnoses them if no signal follows.
> - **Retry bounds:** at most 3 re-refreshes run per episode, backing off 2 s, 4 s and 8 s. A clean RefreshEnd resets
> the budget. Once the budget is spent, a further loss gets a new budget only after a 5-minute cool-down.
> - **RefreshRequired:** its sweeps are coalesced while one is queued. The alarm event item asks for a
> queue of 10,000 (previously 1,000); the server may revise it down. Counters on `otopcua.driver.opcuaclient.events`:
> `alarm_event_queue_overflow` and `alarm_refresh_lossy`.
> Last-told state survives a re-initialise (the host re-subscribes the same set over nodes it did not rebuild) and is
@@ -505,11 +505,22 @@ conditions re-report (`NM:5639-5651`). Some state changes never produce an event
one monitored item. This is how a gap the SDK gave up on is detected. SDK 2.0.1 reports that skip to nothing
else: its `PublishStateChangedMask.Republish` callback fires only in a worker pass that also delivers a message,
which the blocking gap prevents (verified by decompiling, and pinned by a real-SDK test).
- A lossy bracket re-runs ConditionRefresh on that subscription. This is bounded: at most 3 re-refreshes per lossy
episode, backing off 2 s, 4 s and 8 s, and a clean RefreshEnd resets the budget. A RefreshRequired keeps its own
sweep. When the budget is spent, nothing signals. The conditions stay unknown (`Uncertain`) and F5's
`ConditionKnownSignalTimeout` diagnostic reports them. They are never declared known-inactive, and the
`Unsignalable` backstop is not used for this.
- A RefreshStart that arrives while a bracket is still open is itself a loss. It never erases the open bracket's
loss.
- **A loss after the signal withdraws "known"** (review r1). This covers an overflow or sequence gap outside a
bracket, which can hold a live Raise. The driver raises `AlarmInitialStateEventArgs.Withdrawn` once per signalled
episode, ordered like the signal. The host sends `MarkConditionsKnown(Known: false)` for exactly those
conditions, removes them from the backstop, and puts them back in the F5 window. A clean RefreshEnd, or a
transition, makes them known again.
- Every loss, inside or outside a bracket, re-runs ConditionRefresh on that subscription. A failed ConditionRefresh
call is retried the same way. The retries are bounded:
- at most 3 per lossy episode, backing off 2 s, 4 s and 8 s;
- a clean RefreshEnd ends the episode and resets the budget;
- once the budget is spent, a further loss gets a new budget only after a 5-minute cool-down. So the driver
never storms the upstream and never gives up permanently.
- A RefreshRequired keeps its own sweep, now coalesced while one is queued.
- With no signal, the conditions stay unknown (`Uncertain`) and F5's `ConditionKnownSignalTimeout` diagnostic
reports them. They are never declared known-inactive, and the `Unsignalable` backstop is not used for this.
- The alarm event item's queue is now 10,000 (it was 1,000), so overflow is rarer. The detection above is the fix.
- Live on `opc-plc --alm` with a test-sized queue: the overflow notices reach the driver. With `DiscardOldest`
false the RefreshEnd is itself dropped. With `DiscardOldest` true the RefreshEnd arrives over a lossy replay and
@@ -113,6 +113,35 @@ Two observations from the runs:
gap. So can an SDK sequence resync after a transfer. Either costs one bounded re-refresh and is never a wrong signal.
- **Stale RefreshEnd.** If the previous replay's lossy RefreshEnd is still in flight when a re-refresh starts, it can
request one extra refresh. That stays inside the bound.
- **Live losses after the conditions are known.** An overflow outside a bracket is repaired by a re-refresh. A gap
- ~~**Live losses after the conditions are known.** An overflow outside a bracket is repaired by a re-refresh. A gap
outside a bracket is not detected, but the next bracket replays the full state. In both cases conditions that are
already known are not taken back to unknown.
already known are not taken back to unknown.~~ Superseded by review round 1 (below): both are losses, re-refreshed,
and they withdraw "known".
## Review round 1 (2026-11-28)
| # | Finding | Outcome |
|---|---|---|
| 1 | A sequence gap outside a bracket was detected and ignored, although it can hold a live Raise. | **Fixed.** Every newer, non-successor sequence number is a loss, inside or outside a bracket. It withdraws "known" if the subscription signalled, and requests a re-refresh from the same budget. |
| 2 | A loss after the signal never withdrew "known". The budget was shared across episodes. | **Fixed with a new host contract.** `AlarmInitialStateEventArgs.Withdrawn` is an additive constructor flag, exclusive with `Unsignalable`. The driver raises it once per signalled episode, on the first loss, ordered like the signal. `DriverHostActor` turns it into `MarkConditionsKnown(Known: false)` for exactly those conditions (the existing withdraw primitive), removes them from the backstop, and re-adds them to the F5 pending set. The budget was already per episode, because a clean RefreshEnd resets it. A spent budget now also renews after a 5-minute cool-down (`AlarmRefreshRetryCooldown`). While spent, a loss leaves the request open (`ScheduleAlarmRefreshRetryLocked` returns false), so the post-cool-down loss can start a retry. |
| 3 | A nested RefreshStart erased the outer bracket's loss. | **Fixed.** A Start while a bracket is open is recorded as a loss with `??=`, so it never clears the existing one. A depth counter was rejected: in the serial case a lost RefreshEnd would keep the depth above zero forever, so nothing would ever signal. |
| 4 | A failed ConditionRefresh (at subscribe, in a sweep, or in a retry) was never retried. | **Fixed.** The catch in `TryConditionRefreshAsync` calls `NoteConditionRefreshFailed`, which uses the same bounded retry. |
| 5 | A retry's `finally` cleared a `RetryPending` that a newer retry had set. | **Fixed** with a generation token (`RetryGeneration`). |
| 6 | Backward sequence numbers were written back, which produced a second, false gap. | **Fixed.** A repeated or older number (wrap-aware, forward distance ≥ 2³¹) is ignored and not recorded. |
| 7 | The RefreshRequired sweep was unbounded. | **Coalesced**: a RefreshRequired is a no-op while a sweep is queued and not yet started. It is not rate-limited, so a server that sends RefreshRequired continuously still gets back-to-back sweeps. That limit existed before this change and is reported as a follow-up. |
**Red checks.** One mutation per fix, each run against the new tests:
- M1 (gap outside a bracket ignored): `Sequence_gap_outside_a_bracket_withdraws_known_and_re_refreshes` fails.
- M2 (no withdraw): the 3 withdraw tests fail.
- M3 (a nested Start erases the loss): `An_overlapping_RefreshStart_does_not_erase_the_loss` fails.
- M4 (no generation token): `A_retry_scheduled_during_a_refresh_call_keeps_coalescing` fails.
- M5 (older numbers recorded): `An_older_sequence_number_is_ignored_and_not_recorded` fails.
- M6 (a failed refresh is not retried): `A_failed_ConditionRefresh_is_retried_bounded` fails.
- M7 (host ignores `Withdrawn`): Runtime `A_withdrawn_signal_takes_known_back_and_rearms_F5_until_the_next_signal` and
`A_withdrawal_cancels_the_backstop_for_its_conditions` fail.
M6 goes through the `ReportRefreshFailedForTest` seam. The catch that calls it is covered by review.
**Residual.** If a subscription never signalled (its initial refresh is still pending), a loss withdraws nothing,
because nothing was known.