Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Stop everything the dispatcher started when it stops - #334

Open
Marenz wants to merge 4 commits into
frequenz-floss:v1.x.xfrom
Marenz:fix-shutdown-cleanup
Open

Marenz wants to merge 4 commits into
frequenz-floss:v1.x.xfrom
Marenz:fix-shutdown-cleanup

Conversation

@Marenz

@Marenz Marenz commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Stopping the dispatcher or the ActorDispatcher left things running:

  • managed dispatch types were not stopped, and waiting for the dispatcher could block forever;
  • the dispatch stream task kept calling the API after the scheduler stopped;
  • actors started by an ActorDispatcher kept running, with nobody left to stop them.

Each stop now runs on its own, so one failure or a cancellation cannot skip the rest. Tests are in tests/test_shutdown.py.

`ContinuationCapable` and the outcome enum used with it sat in
`_actor_dispatcher.py`, while everything else about continuations already
lives in `_continuation.py`. Moving them puts the whole opt-in feature in one
module, and leaves room in `_actor_dispatcher.py`, which is close to the
line limit its own checks enforce.

This is a move and nothing else. The classes are unchanged, and
`ContinuationCapable` is still exported under the same public name.

Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
`ActorDispatcher` only stopped an actor when a stop event for its dispatch
arrived. Stopping the actor dispatcher itself left every actor it had
started running, and nothing else can stop them: the actor dispatcher
created them and owns the channels they get their dispatch updates from. So
an actor kept acting on its dispatch until the process ended.

The actors are now stopped, and their update channels closed, whenever the
actor dispatcher's own task ends, whether it was cancelled or its running
status receiver was closed.

Every actor is stopped on its own, and closing a channel is a step of its
own too, so an actor that fails to stop can neither leave the others running
nor keep any channel open. A cancellation in the middle of all that does not
end it either: the stops that are left still run, and the cancellation is
raised once they are done. This also covers stopping a single actor on a
dispatch stop event, where the actor is already out of the dispatcher's
bookkeeping and nothing else could reach its channel any more.

A failure cannot be raised while the service is stopping, because that would
replace the cancellation that asked it to stop. It is kept and reported by
`wait()` instead, so it also reaches a caller that only ever uses `stop()`.

Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
`Dispatcher.cancel()` cancelled the actor dispatchers it manages, but it
never set the event that `wait()` blocks on, and it never removed the
managed types from its list.

The first `stop()` still returned, because waiting for the cancelled
background service raises and cuts the wait short. Everything after it
blocked forever though: a second `stop()`, `wait()`, and awaiting the
dispatcher. The stopped types were also still reported as managed, and the
actor dispatchers were only asked to stop, never waited for, so the actors
they had started were left running.

`cancel()` now sets the event, and `wait()` stops and waits for whatever
managed types are left before dropping them. Each type is stopped on its
own, so one type that fails to stop cannot leave another one running, and
the failures do not replace the error that is already on its way out, which
is normally the cancellation itself.

A task that is already being cancelled is never cancelled again, neither by
`cancel()`, nor by `wait()`, nor by `stop_managing()`. A second cancellation
interrupts the cleanup that the first one started, which used to leave a
type's actors half stopped, and in the case of `stop_managing()` left them
running with nothing able to reach them any more.

Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
The client creates one stream helper per microgrid and keeps it in a list of
its own. Disconnecting the client only closes the gRPC channel, so the
helper's task stayed alive and kept calling the dispatch API again and again
after the scheduler was already gone.

The client has no public way to stop it, so the scheduler now stops the
stream it asked for itself. The stream receiver is closed first, so it is
not left reading from a channel that is closed under it.

Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
@Marenz
Marenz requested a review from a team as a code owner November 30, 2026 11:09
@Marenz
Marenz requested review from stefan-brus-frequenz and removed request for a team November 30, 2026 11:09
@github-actions github-actions Bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:dispatcher Affects the high-level dispatcher interface labels Nov 30, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:dispatcher Affects the high-level dispatcher interface part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant