Fix blocking sync I/O on the event loop across containers, jobs, and xmlrpc

The container/workspace reachability probes (socket connect + HTTP check)
ran synchronously with real timeouts inside async request handlers and the
live-view relay's 3-4s ticks, freezing the whole event loop whenever a
container wasn't cleanly reachable - the likely cause of the periodic
app-wide stalls. Converted the probe chain (api._port_reachable/_http_probe,
editor_reachable, instance_runtime, provision.editor_ready/view) to real
async I/O and parallelized the admin container/workspace list decorators.

Also fixes: XmlrpcService.on_disable blocked up to 10s on a synchronous
subprocess.wait inside an async method (now matches TelegramService's
async-subprocess pattern); JobService._sweep_expired ran every job kind's
cleanup() - including shutil.rmtree on large directories - synchronously
on every tick, now offloaded via asyncio.to_thread for all job kinds at
once; and several smaller blocking reads/writes on request/service paths
(attachment-to-gitea mirroring, stealth chunked downloads, dbapi/isslop
file reads, job payload/report I/O) moved off the loop thread.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-03 12:59:10 +00:00
co-authored by Claude Sonnet 5
parent cc969aa187
commit 4a13415b43
23 changed files with 132 additions and 104 deletions
+15 -11
View File
@@ -59,15 +59,17 @@ class DeepsearchService(JobService):
payload = dict(job.get("payload", {}))
query = payload.get("query", "")
output_dir = self.session_dir(uid)
output_dir.mkdir(parents=True, exist_ok=True)
(output_dir / "control.json").write_text(
json.dumps({"state": "running"}), encoding="utf-8"
await asyncio.to_thread(output_dir.mkdir, parents=True, exist_ok=True)
await asyncio.to_thread(
(output_dir / "control.json").write_text,
json.dumps({"state": "running"}),
encoding="utf-8",
)
payload["collection"] = self.collection_name(uid)
payload["model"] = self.get_config()["deepsearch_model"] or INTERNAL_MODEL
payload["cached_hashes"] = self._cached_hashes(database)
payload_path = output_dir / "payload.json"
payload_path.write_text(json.dumps(payload), encoding="utf-8")
await asyncio.to_thread(payload_path.write_text, json.dumps(payload), encoding="utf-8")
actor_kind = job.get("owner_kind") or "system"
actor_uid = job.get("owner_id") if job.get("owner_kind") == "user" else None
@@ -89,11 +91,11 @@ class DeepsearchService(JobService):
metadata={"query": query, "error": str(exc)[:200]},
links=[audit.job(uid)],
)
shutil.rmtree(output_dir, ignore_errors=True)
await asyncio.to_thread(shutil.rmtree, output_dir, ignore_errors=True)
raise
report = self._load_report(output_dir)
self._persist_cache(database, output_dir)
report = await self._load_report(output_dir)
await self._persist_cache(database, output_dir)
from datetime import datetime, timezone
database.update_deepsearch_session(
@@ -158,12 +160,13 @@ class DeepsearchService(JobService):
if row.get("url_hash")
]
def _persist_cache(self, database, output_dir: Path) -> None:
async def _persist_cache(self, database, output_dir: Path) -> None:
path = output_dir / "url_cache.json"
if not path.is_file():
return
try:
entries = json.loads(path.read_text(encoding="utf-8"))
text = await asyncio.to_thread(path.read_text, encoding="utf-8")
entries = json.loads(text)
except (ValueError, OSError):
return
for entry in entries:
@@ -244,12 +247,13 @@ class DeepsearchService(JobService):
)
return summary
def _load_report(self, output_dir: Path) -> dict:
async def _load_report(self, output_dir: Path) -> dict:
report_path = output_dir / "report.json"
if not report_path.is_file():
return {}
try:
return json.loads(report_path.read_text(encoding="utf-8"))
text = await asyncio.to_thread(report_path.read_text, encoding="utf-8")
return json.loads(text)
except (ValueError, OSError):
return {}