Skip to content

Commit c2feeef

Browse files
agnersclaude
andcommitted
Wait for startup to complete before running shutdown
If PrepareForShutdown fires while Supervisor is still starting up, wait for startup to complete before running the graceful shutdown sequence. This prevents shutting down partially initialized containers and services. The _startup_complete event is set automatically in set_state() whenever state transitions to RUNNING. If SIGTERM arrives during startup, core.stop() cancels the monitor task via host.unload(), cleanly interrupting the wait. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 3b2e4c7 commit c2feeef

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

supervisor/core.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def __init__(self, coresys: CoreSys) -> None:
4242
self._state: CoreState = CoreState.INITIALIZE
4343
self.exit_code: int = 0
4444
self._shutdown_event: asyncio.Event = asyncio.Event()
45+
self._startup_complete: asyncio.Event = asyncio.Event()
4546

4647
@property
4748
def state(self) -> CoreState:
@@ -82,6 +83,9 @@ async def set_state(self, new_state: CoreState) -> None:
8283
self._state = new_state
8384
await self._write_run_state()
8485

86+
if self._state == CoreState.RUNNING:
87+
self._startup_complete.set()
88+
8589
# Don't attempt to notify anyone on CLOSE as we're about to stop the event loop
8690
if self._state != CoreState.CLOSE:
8791
self.sys_bus.fire_event(BusEvent.SUPERVISOR_STATE_CHANGE, self._state)
@@ -360,6 +364,12 @@ async def shutdown(self, *, remove_homeassistant_container: bool = False) -> Non
360364
Reentrant: if a shutdown is already in progress, subsequent calls
361365
await completion of the existing shutdown rather than starting a second one.
362366
"""
367+
if self.state in STARTING_STATES:
368+
_LOGGER.debug(
369+
"Shutdown requested while Supervisor is still starting up, waiting for startup to complete"
370+
)
371+
await self._startup_complete.wait()
372+
363373
# Supervisor is already tearing down, no point running shutdown
364374
if self.state in (CoreState.STOPPING, CoreState.CLOSE):
365375
_LOGGER.warning("Ignoring shutdown request, Supervisor is already stopping")

0 commit comments

Comments
 (0)