Stopping work when a flow ends
A flow can start work that outlives a step: a pairing run, an open connection, a busy adapter. When the host drops
the flow without the plugin having ended it, that work keeps running unless the plugin is told. Two optional
members of Reeve.PluginAbstractions tell it:
ValueTask IInstanceActionFlow.OnFlowEndedAsync(string flowId, ConfigFlowEndReason reason, CancellationToken ct);
ValueTask IConfigFlowFactory.OnFlowEndedAsync(string flowId, ConfigFlowEndReason reason, CancellationToken ct);
Both are default interface members that do nothing. A plugin that does not override them is never told. Keep a fallback for the cases the host cannot report (below).
When it is called
Every end of a flow, including one the plugin reports itself, first takes the flow's end claim: one Redis set-if-absent, taken the moment the end is decided and before the host does anything else with it (creating or updating an instance included). The end that wins the claim is the flow's end; one that loses it does nothing. Only a winning end that is not the plugin's own tells the plugin, and the end does not wait for it: the notice is handed to a host-lifetime runner, so no request waits for a plugin. A local hook can therefore start before the operator's abort has answered. A flow whose end is claimed takes no further step, save or abort (410), even if its state could not be deleted.
Because delivery is detached, a notice can arrive late: up to the hook bound plus the relay's 4 s overhead (9 s by
default), and a second hook bound after that, so the operator may already have started a new flow. A hook that stops a
resource shared between flows (a commissioning window, a pairing run) must check that the resource still belongs to
flowId before it stops it.
| How the flow ends | Hook | Reason | Runs on | Notes |
|---|---|---|---|---|
The plugin returns Finish |
no | Claimed when the result arrives. If an abort claimed first (it landed while the step ran), the Finish is discarded: no instance is created or updated, and the submit answers 410. |
||
The plugin returns Error |
no | Claimed likewise; an Error that lost to an abort is discarded and answers 410. |
||
A Finish whose instance create or update fails |
no | The plugin reported Finish, so it is not told; the operator sees the failure. |
||
| The operator aborts | yes | Aborted |
The replica that owns the flow (the one running its steps) when it answers "done" to the relayed request (a fenced owner answers "busy" and runs nothing); otherwise the replica that handled the abort, on its own live instance. | The abort answers as soon as it has claimed the end and deleted the flow. Delivery then runs in the background: the owner gets the hook bound plus the relay's overhead, and any answer but "done" is followed by a local call. An abort of a flow that already ended answers 410 and calls nothing. |
| The operator aborts while a step is still running | yes | Aborted |
As above. | The hook runs while SubmitAsync is still in flight. What that step then returns (Finish included) is discarded. |
A step runs past the submit time bound (30 s, 180 s for a Polling step) |
yes | TimedOut |
The replica running the flow's steps. | The step's own task is abandoned, not stopped; the hook is how the plugin stops what it started. |
SubmitAsync throws |
yes | Failed |
The replica running the flow's steps. | The exception type is audited, never its message. |
| The host cannot decrypt the flow's stored answers or OAuth tokens | yes | Failed |
The replica running the flow's steps. | |
| The flow reaches its deadline (30 minutes without a submit or poll) | yes | Expired |
The replica that leads flow reconciliation, at the first sweep after the deadline, delivered like an abort: the owner first, otherwise there. | The deadline is recorded when the state is written, in an index the leader sweeps once a minute, at most 200 flows a pass. A submit that lands before the sweep reaches the flow continues it and moves the deadline. See below for what the sweep cannot do. |
| A new flow starts while another is open | no | The host has no replacement: each start is an independent flow, up to a per-user cap. A plugin that allows one run at a time decides what to do with the older one in its own SubmitAsync. |
||
| The replica running the flow stops | no | The state stays in Redis and the next submit is adopted by another replica. A flow nobody resumes reaches its deadline, which is the Expired row. |
||
| The host is shutting down | A hook that is running is cancelled through its token, and one that starts during shutdown gets a token already cancelled. A notice not yet delivered is lost (below); the replica's own flows are adopted or reach their deadline. | |||
| The plugin instance stops, reloads or is uninstalled while a flow is open | no | There is no live plugin to call. A later expiry finds none or a new incarnation, which must ignore a flow id it holds nothing for. | ||
| The client disconnects mid-step | no | The flow stays for its next submit. |
What is guaranteed, and what is not
-
At most once per flow, across replicas. Two Redis keys do it. The end claim decides which end of the flow wins; the call claim, taken by whichever replica is about to call the plugin (the owner, or the replica that decided the end, as the fallback), decides that one call is made. A fallback runs after any answer other than "done" (owner gone, request not taken, no answer, owner error, owner fenced and answering "busy") and still cannot repeat a call an owner already started. The cost is the other direction: an owner that took the call claim and then died mid-hook is not called again, so the plugin is told at most once, not exactly once.
-
Never after the plugin's own
FinishorError. That end is claimed when it is decided, so no abort, timeout or expiry tells the plugin afterwards, however long the host then spends creating the instance. -
Delivered on the owner when it answers, otherwise locally. The owner is the replica that ran the flow's steps and holds its in-memory work. It is asked first and given the hook bound plus the relay's 4 s overhead (a 2 s roster read and a 2 s share of the ask, 9 s by default); a replica that does not answer in that time is given up on and the deciding replica calls its own live instance.
-
The hook is bounded (
ConfigFlow:EndHookTimeoutMs, default 5 s, per call). It receives a token cancelled at that bound or when the host stops; a hook that outlasts the bound is abandoned, and a throw is logged by type only. A hook never fails or delays the operator's abort. -
Expiry is an explicit outcome, not an observation. Every write of a flow's state also records its deadline in a Redis sorted set. The reconciliation leader reads the entries that are due, a bounded batch a pass. For each it checks that the state still exists: a flow a normal end already deleted only loses its entry; any other takes the end claim as
Expired, deletes the state and its entry, then delivers. If another end holds the claim, the flow is not expired and the leftover state is removed without a call. Two sweepers that race produce one call, because the claim decides. -
Best-effort gaps, where the plugin is not told:
- the host stops after an end was claimed and before its notice was delivered (a delivery that starts while the host is stopping runs no hook and leaves the call claim free);
- the sweeper is down, or a backlog of due flows is longer than the sweep clears, for longer than the state's backstop
(
ConfigFlow:ExpiryBackstopSeconds, default 1 h past the deadline), after which the state is dropped by Redis with no end; - the index entry is written in a second step after the state, so a crash or a Redis error between the two leaves a flow unindexed (it vanishes at the backstop, nobody told) or indexed with an older deadline (it ends early);
- Redis is flushed, or fails when the claim is taken (a flush can also allow a second call). Two consecutive failures,
of the claim and then of the state's delete, can let a later sweep report
Expiredfor a flow the plugin had finished.
Keep a deadline of your own for the work (Matter keeps one for pairing) and treat the hook as the fast path.
-
Both claims live at least as long as the flow's own state can (the flow's deadline and a backstop more), so the sweep never reads a lapsed claim beside a surviving state. They are not a durable record, so make the stop idempotent.
-
Do not rely on the hook seeing the flow's in-memory state: a fallback, an expiry, or a flow adopted from a stopped replica runs elsewhere. Stop the work through something every replica reaches, such as the adapter or a shared store, keyed by the flow id.
-
Ignore a flow id you hold nothing for. The hook can arrive after the plugin already finished its work, or for a flow that ran on another replica.
-
Do not log or audit anything secret from the hook. It receives no answers.
Timing and ownership
- The relay hook is bounded, with 4 seconds of overhead on top of the bound. Both claims outlive the state's TTL backstop.
- A forwarded submit not taken within 5 seconds is withdrawn and answers 503.
- A Finish refused after the plugin's external effect leaves that effect without an instance.