Files
AstrBot/runtime_bootstrap.py
邹永赫 7e9d8599f1 fix: capture runtime bootstrap logs after logger init
- Add bootstrap record buffer in runtime_bootstrap for early TLS patch logs before logger is ready.

- Flush buffered bootstrap logs to astrbot logger at process startup in main.py.

- Include concrete exception details for TLS bootstrap failures to improve diagnosis.
2026-02-10 15:27:59 +09:00

68 lines
1.8 KiB
Python

import ssl
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
import aiohttp.connector as aiohttp_connector
import certifi
@dataclass
class BootstrapRecord:
level: str
message: str
_BOOTSTRAP_RECORDS: list[BootstrapRecord] = []
def _record(level: str, message: str) -> None:
_BOOTSTRAP_RECORDS.append(BootstrapRecord(level=level, message=message))
def flush_bootstrap_records(log_obj: Any) -> None:
if not _BOOTSTRAP_RECORDS:
return
level_methods: dict[str, Callable[[str], Any]] = {
"info": getattr(log_obj, "info", None),
"warning": getattr(log_obj, "warning", None),
"error": getattr(log_obj, "error", None),
}
fallback = getattr(log_obj, "info", None)
for record in _BOOTSTRAP_RECORDS:
logger_method = level_methods.get(record.level, fallback)
if callable(logger_method):
logger_method(record.message)
_BOOTSTRAP_RECORDS.clear()
def configure_runtime_ca_bundle() -> None:
try:
_record("info", "Bootstrapping runtime CA bundle.")
ssl_context = ssl.create_default_context()
ssl_context.load_verify_locations(cafile=certifi.where())
if hasattr(aiohttp_connector, "_SSL_CONTEXT_VERIFIED"):
aiohttp_connector._SSL_CONTEXT_VERIFIED = ssl_context
_record(
"info",
"Configured aiohttp verified SSL context with system+certifi trust chain.",
)
else:
_record(
"warning",
"aiohttp connector does not expose _SSL_CONTEXT_VERIFIED; skipped patch.",
)
except Exception as exc:
_record(
"error",
f"Failed to configure runtime CA bundle for aiohttp: {exc!r}",
)
return
configure_runtime_ca_bundle()