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>
This commit is contained in:
copilot-swe-agent[bot]
2026-04-15 16:09:39 +00:00
committed by GitHub
parent 587f2a09f8
commit bb07fbd2c3

View File

@@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio import asyncio
import base64 import base64
import contextlib
import hashlib import hashlib
import hmac import hmac
import json import json
@@ -60,6 +61,7 @@ _domain_reachability_cache_lock = Lock()
_DOMAIN_REACHABILITY_TTL = 60 _DOMAIN_REACHABILITY_TTL = 60
_DOMAIN_REACHABILITY_STARTUP_DELAY = 5 _DOMAIN_REACHABILITY_STARTUP_DELAY = 5
_domain_reachability_task: asyncio.Task | None = None _domain_reachability_task: asyncio.Task | None = None
_domain_reachability_task_lock = asyncio.Lock()
BACKUP_LOG = "/var/log/sovran-hub-backup.log" BACKUP_LOG = "/var/log/sovran-hub-backup.log"
BACKUP_STATUS = "/var/log/sovran-hub-backup.status" BACKUP_STATUS = "/var/log/sovran-hub-backup.status"
@@ -4426,5 +4428,19 @@ async def _background_domain_reachability_checker():
async def _startup_domain_reachability(): async def _startup_domain_reachability():
"""Start the background domain reachability checker.""" """Start the background domain reachability checker."""
global _domain_reachability_task global _domain_reachability_task
if _domain_reachability_task is None or _domain_reachability_task.done(): async with _domain_reachability_task_lock:
_domain_reachability_task = asyncio.create_task(_background_domain_reachability_checker()) 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