@@ -110,6 +110,7 @@ def __init__(
110110 self .store = store
111111 self .config = config or OrchestratorConfig ()
112112 self .controllers : dict [str , Any ] = {}
113+ self ._tasks : list [asyncio .Task [None ]] = []
113114
114115 def _create_controllers (self ) -> None :
115116 """Create and initialize all controllers."""
@@ -141,8 +142,12 @@ async def start(self) -> None:
141142 if self .controllers :
142143 return
143144
144- _LOGGER .info ("Starting orchestrator" )
145145 self ._create_controllers ()
146+ self ._tasks .append (
147+ get_task_service ().create_background_task (
148+ self ._log_active_tasks (), "log-active-tasks"
149+ )
150+ )
146151
147152 async def stop (self ) -> None :
148153 """Stop the orchestrator and all controllers."""
@@ -152,9 +157,15 @@ async def stop(self) -> None:
152157 _LOGGER .info ("Stopping orchestrator" )
153158
154159 # Stop controllers in reverse order
160+ close_tasks = []
155161 for name , controller in reversed (self .controllers .items ()):
156162 _LOGGER .debug ("Stopping controller: %s" , name )
157- await controller .close ()
163+ close_tasks .append (controller .close ())
164+ await asyncio .gather (* close_tasks , return_exceptions = True )
165+
166+ for task in self ._tasks :
167+ task .cancel ()
168+ await asyncio .gather (* self ._tasks , return_exceptions = True )
158169
159170 # Wait for all tasks to complete
160171 await get_task_service ().block_till_done ()
@@ -332,3 +343,14 @@ async def run(self) -> None:
332343 raise FluxException (f"Uncaught error in orchestrator: { err } " ) from err
333344 finally :
334345 await self .stop ()
346+
347+ async def _log_active_tasks (self ) -> None :
348+ """Log active tasks periodically."""
349+ task_service = get_task_service ()
350+ while True :
351+ await asyncio .sleep (5 )
352+ active_tasks = task_service .get_active_tasks ()
353+ if active_tasks :
354+ _LOGGER .debug ("Active tasks (%d):" , len (active_tasks ))
355+ for task in active_tasks :
356+ _LOGGER .debug (" %s" , task .get_name ())
0 commit comments