Fix RuntimeError: Event loop is closed

- Move HTTP client cleanup into the same async context
- Prevents 'Event loop is closed' error when closing HTTP client
- Ensures proper resource cleanup in the same event loop
This commit is contained in:
daymade
2025-12-20 23:29:28 +08:00
parent 6a94cfcc69
commit dbcb53a376

View File

@@ -148,11 +148,14 @@ class AIProcessorAsync:
CRITICAL FIX (P1-3): Ensures HTTP client cleanup
"""
# Run async processing in sync context
try:
return asyncio.run(self._process_async(text, context))
finally:
# Ensure HTTP client is closed
asyncio.run(self._close_http_client())
async def _run_with_cleanup():
try:
return await self._process_async(text, context)
finally:
# Ensure HTTP client is closed in the same event loop
await self._close_http_client()
return asyncio.run(_run_with_cleanup())
async def _process_async(self, text: str, context: str) -> Tuple[str, List[AIChange]]:
"""