Skip to content

fix(engine): close AsyncAnthropic clients to prevent event loop shutdown crash (fixes #506)#508

Open
mickume wants to merge 1 commit intodevelopfrom
fix/issue-506-event-loop-shutdown-crash
Open

fix(engine): close AsyncAnthropic clients to prevent event loop shutdown crash (fixes #506)#508
mickume wants to merge 1 commit intodevelopfrom
fix/issue-506-event-loop-shutdown-crash

Conversation

@mickume
Copy link
Copy Markdown
Contributor

@mickume mickume commented Apr 22, 2026

Summary

Close all AsyncAnthropic clients after use via try/finally blocks. The Anthropic SDK wraps an httpx AsyncClient internally; when these clients were garbage-collected after the event loop closed, their __del__ scheduled aclose() tasks — producing two RuntimeError: Event loop is closed tracebacks on shutdown.

Closes #506

Changes

File Change
agent_fox/core/client.py Wrap ai_call()'s inner _call() in try/finally to close the client
agent_fox/knowledge/consolidation.py Wrap _call_llm_json() body in try/finally to close the client
agent_fox/nightshift/categories/quality_gate.py Close locally-created client (skip when backend is injected)
tests/unit/core/test_client_close.py 4 regression tests verifying clients are closed

Tests

  • TestAiCallClosesClient::test_client_closed_on_success
  • TestAiCallClosesClient::test_client_closed_on_api_error
  • TestConsolidationCallLlmJsonClosesClient::test_client_closed_after_llm_call
  • TestConsolidationCallLlmJsonClosesClient::test_client_closed_on_llm_error

Verification

  • All existing tests pass: ✅ (5242 unchanged)
  • New tests pass: ✅ (4 added)
  • Linter / formatter: ✅
  • No regressions: ✅

Auto-generated by af-fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant