Skip to content

Conversation

@Marenz
Copy link
Contributor

@Marenz Marenz commented May 5, 2025

fixes #145

Copy link
Contributor

@llucax llucax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a safer/more complete/future-proof approach. Didn't think too much about the order, but I think it should be fine.

@github-actions github-actions bot added part:docs Affects the documentation part:dispatcher Affects the high-level dispatcher interface labels May 5, 2025
Copy link
Contributor

@llucax llucax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @ela-kotulska-frequenz pointed out this also misses disconnecting at stop. If we implement it there, it should come for free to __aexit__() as the default implementation of __aexit__() for BackgroundService is to call stop() (stop() calls self.cancel() + await self.wait()).

For now a "simple" solution when we need to do something async in cancel() (like the call to self._client.disconnect() would be to store the future, like:

def cancel():
    self._disconnecting_future = self._client.disconnect()

def wait():
    if self._disconnecting_future is not None:
        await self._disconnecting_future
    await super(self).wait()

Not super nice, as the disconnection won't really happen until the await, I think, but it should work for most cases. To do it right we would need to spawn a new task just to do the disconnection :-/

@Marenz Marenz force-pushed the cleanup-disc branch 3 times, most recently from 070b1e1 to 12b5e43 Compare May 5, 2025 10:40
@Marenz Marenz marked this pull request as ready for review May 5, 2025 10:40
@Marenz Marenz requested a review from a team as a code owner May 5, 2025 10:40
@Marenz Marenz requested review from llucax and stefan-brus-frequenz and removed request for stefan-brus-frequenz May 5, 2025 10:40
@Marenz
Copy link
Contributor Author

Marenz commented May 5, 2025

Still missing the stop() part

Comment on lines 370 to 384
async def stop(self, msg: str | None = None) -> None:
"""Stop the local dispatch service and initiate client disconnection.
This method is called when the dispatcher is stopped.
Args:
msg: The message to log.
"""
_logger.debug("Stopping dispatcher")
await self._bg_service.stop(msg)

for type in self._actor_dispatchers.keys():
await self.stop_managing(type)

await self._client.disconnect()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you "fixed" cancel() and wait() there should be no need to override stop() too. In this case I guess you should move the actor stopping to cancel() too. And once you do that, maybe it makes sense to actually spawn a new task to do the cleanup. not sure, I guess you can also have a list of futures to wait for in the wait(), but you will not actually initiate the cleanup upon calling cancel(), only when calling wait().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm so basically just remove stop() now? The actor stopping is already in cancel (using .cancel)..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I guess so, I hope it works. Stopping/cleanup is pretty difficult/complicated at the moment, I hope we can improve things soon.

Signed-off-by: Mathias L. Baumann <[email protected]>
@Marenz Marenz requested a review from llucax May 5, 2025 18:49
Copy link
Contributor

@llucax llucax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I praise the lord that this works 🙏 😆

@Marenz Marenz added this pull request to the merge queue May 6, 2025
Merged via the queue into frequenz-floss:v0.x.x with commit 3f17316 May 6, 2025
5 checks passed
@Marenz Marenz deleted the cleanup-disc branch May 6, 2025 08:48
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dispatcher doesn't disconnect with client during cleanup

2 participants