Skip to content

Commit 52c78a0

Browse files
Improve BackgroundService destructor to avoid unnecessary cancel calls
Previously, BackgroundService.__del__() would always call cancel() regardless of whether the service was still running. This caused missleading messages suggesting services weren't properly stopped. Services should always be explicitly stopped before deletion, so destructor now only calls cancel() if the service is still running. The error log can help identify and clean up such cases. Signed-off-by: Elzbieta Kotulska <elzbieta.kotulska@frequenz.com>
1 parent 713d7b5 commit 52c78a0

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/frequenz/sdk/actor/_background_service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,9 @@ def __del__(self) -> None:
278278
279279
Cancel all running tasks spawned by this background service.
280280
"""
281-
self.cancel("{self!r} was deleted")
281+
if self.is_running:
282+
_logger.error("%s was not stopped, before delete", repr(self))
283+
self.cancel(f"{self!r} was deleted")
282284

283285
def __repr__(self) -> str:
284286
"""Return a string representation of this instance.

0 commit comments

Comments
 (0)