From bb07fbd2c30b837910718bf2d5c0432bde29c85a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Apr 2026 16:09:39 +0000 Subject: [PATCH] Add safe startup/shutdown lifecycle for domain reachability task Agent-Logs-Url: https://github.com/naturallaw777/staging_alpha/sessions/038b6d9a-0298-41d7-949f-40069cd3320f Co-authored-by: naturallaw777 <99053422+naturallaw777@users.noreply.github.com> --- app/sovran_systemsos_web/server.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app/sovran_systemsos_web/server.py b/app/sovran_systemsos_web/server.py index 911ea89..07c81dd 100644 --- a/app/sovran_systemsos_web/server.py +++ b/app/sovran_systemsos_web/server.py @@ -4,6 +4,7 @@ from __future__ import annotations import asyncio import base64 +import contextlib import hashlib import hmac import json @@ -60,6 +61,7 @@ _domain_reachability_cache_lock = Lock() _DOMAIN_REACHABILITY_TTL = 60 _DOMAIN_REACHABILITY_STARTUP_DELAY = 5 _domain_reachability_task: asyncio.Task | None = None +_domain_reachability_task_lock = asyncio.Lock() BACKUP_LOG = "/var/log/sovran-hub-backup.log" BACKUP_STATUS = "/var/log/sovran-hub-backup.status" @@ -4426,5 +4428,19 @@ async def _background_domain_reachability_checker(): async def _startup_domain_reachability(): """Start the background domain reachability checker.""" global _domain_reachability_task - if _domain_reachability_task is None or _domain_reachability_task.done(): - _domain_reachability_task = asyncio.create_task(_background_domain_reachability_checker()) + async with _domain_reachability_task_lock: + if _domain_reachability_task is None or _domain_reachability_task.done(): + _domain_reachability_task = asyncio.create_task(_background_domain_reachability_checker()) + + +@app.on_event("shutdown") +async def _shutdown_domain_reachability(): + """Stop the background domain reachability checker.""" + global _domain_reachability_task + async with _domain_reachability_task_lock: + task = _domain_reachability_task + _domain_reachability_task = None + if task is not None and not task.done(): + task.cancel() + with contextlib.suppress(asyncio.CancelledError): + await task