Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(providers): await cost recording inline to avoid event-loop-closed teardown (#241)
Raghav Chamadiyad8be056a79be
diff --git a/packages/core/src/repowise/core/providers/llm/anthropic.py b/packages/core/src/repowise/core/providers/llm/anthropic.pyindex 70eab6f34..6bd431e2d 100644--- a/packages/core/src/repowise/core/providers/llm/anthropic.py+++ b/packages/core/src/repowise/core/providers/llm/anthropic.py@@ -12,6 +12,7 @@ Recommended models (as of 2026):from __future__ import annotations+import contextlibimport osfrom typing import TYPE_CHECKING, Any, AsyncIterator@@ -184,20 +185,20 @@ class AnthropicProvider(BaseProvider):)if self._cost_tracker is not None:- import asyncio-- try:- asyncio.get_event_loop().create_task(- self._cost_tracker.record(- model=self._model,- input_tokens=result.input_tokens,- output_tokens=result.output_tokens,- operation="doc_generation",- file_path=None,- )+ # Await the cost record inline rather than spawning a detached+ # task. A fire-and-forget create_task can still be flushing its+ # aiosqlite write when the event loop is torn down (e.g. the+ # asyncio.run teardown after doc generation), which surfaces as a+ # noisy "Event loop is closed" worker-thread traceback. record()+ # swallows its own persistence errors, so generation is unaffected.+ with contextlib.suppress(Exception):+ await self._cost_tracker.record(+ model=self._model,+ input_tokens=result.input_tokens,+ output_tokens=result.output_tokens,+ operation="doc_generation",+ file_path=None,)- except RuntimeError:- pass # No running event loop — skip async recordreturn resultdiff --git a/packages/core/src/repowise/core/providers/llm/deepseek.py b/packages/core/src/repowise/core/providers/llm/deepseek.pyindex 3749a4f8a..dfd616613 100644--- a/packages/core/src/repowise/core/providers/llm/deepseek.py+++ b/packages/core/src/repowise/core/providers/llm/deepseek.py@@ -181,17 +181,19 @@ class DeepSeekProvider(BaseProvider):)if self._cost_tracker is not None:- import asyncio-- with contextlib.suppress(RuntimeError):- asyncio.get_event_loop().create_task(- self._cost_tracker.record(- model=self._model,- input_tokens=result.input_tokens,- output_tokens=result.output_tokens,- operation="doc_generation",- file_path=None,- )+ # Await the cost record inline rather than spawning a detached+ # task. A fire-and-forget create_task can still be flushing its+ # aiosqlite write when the event loop is torn down (e.g. the+ # asyncio.run teardown after doc generation), which surfaces as a+ # noisy "Event loop is closed" worker-thread traceback. record()+ # swallows its own persistence errors, so generation is unaffected.+ with contextlib.suppress(Exception):+ await self._cost_tracker.record(+ model=self._model,+ input_tokens=result.input_tokens,+ output_tokens=result.output_tokens,+ operation="doc_generation",+ file_path=None,)return resultdiff --git a/packages/core/src/repowise/core/providers/llm/gemini.py b/packages/core/src/repowise/core/providers/llm/gemini.pyindex 7811b0e59..9ec38f490 100644--- a/packages/core/src/repowise/core/providers/llm/gemini.py+++ b/packages/core/src/repowise/core/providers/llm/gemini.py@@ -11,6 +11,7 @@ Recommended models:from __future__ import annotationsimport asyncio+import contextlibimport loggingimport os@@ -224,18 +225,20 @@ class GeminiProvider(BaseProvider):)if self._cost_tracker is not None:- try:- asyncio.get_event_loop().create_task(- self._cost_tracker.record(- model=self._model,- input_tokens=result.input_tokens,- output_tokens=result.output_tokens,- operation="doc_generation",- file_path=None,- )+ # Await the cost record inline rather than spawning a detached+ # task. A fire-and-forget create_task can still be flushing its+ # aiosqlite write when the event loop is torn down (e.g. the+ # asyncio.run teardown after doc generation), which surfaces as a+ # noisy "Event loop is closed" worker-thread traceback. record()+ # swallows its own persistence errors, so generation is unaffected.+ with contextlib.suppress(Exception):+ await self._cost_tracker.record(+ model=self._model,+ input_tokens=result.input_tokens,+ output_tokens=result.output_tokens,+ operation="doc_generation",+ file_path=None,)- except RuntimeError:- pass # No running event loop — skip async recordreturn resultdiff --git a/packages/core/src/repowise/core/providers/llm/litellm.py b/packages/core/src/repowise/core/providers/llm/litellm.pyindex d4152bb51..22eaf7c45 100644--- a/packages/core/src/repowise/core/providers/llm/litellm.py+++ b/packages/core/src/repowise/core/providers/llm/litellm.py@@ -19,6 +19,7 @@ Reference: https://docs.litellm.ai/docs/providersfrom __future__ import annotations+import contextlibimport osfrom typing import TYPE_CHECKING, Any, AsyncIterator@@ -182,20 +183,20 @@ class LiteLLMProvider(BaseProvider):)if self._cost_tracker is not None:- import asyncio-- try:- asyncio.get_event_loop().create_task(- self._cost_tracker.record(- model=self._model,- input_tokens=result.input_tokens,- output_tokens=result.output_tokens,- operation="doc_generation",- file_path=None,- )+ # Await the cost record inline rather than spawning a detached+ # task. A fire-and-forget create_task can still be flushing its+ # aiosqlite write when the event loop is torn down (e.g. the+ # asyncio.run teardown after doc generation), which surfaces as a+ # noisy "Event loop is closed" worker-thread traceback. record()+ # swallows its own persistence errors, so generation is unaffected.+ with contextlib.suppress(Exception):+ await self._cost_tracker.record(+ model=self._model,+ input_tokens=result.input_tokens,+ output_tokens=result.output_tokens,+ operation="doc_generation",+ file_path=None,)- except RuntimeError:- pass # No running event loop — skip async recordreturn resultdiff --git a/packages/core/src/repowise/core/providers/llm/openai.py b/packages/core/src/repowise/core/providers/llm/openai.pyindex 0b85ede03..f9d5f409e 100644--- a/packages/core/src/repowise/core/providers/llm/openai.py+++ b/packages/core/src/repowise/core/providers/llm/openai.py@@ -12,6 +12,7 @@ Recommended models (as of 2026):from __future__ import annotations+import contextlibimport osfrom typing import TYPE_CHECKING, Any, AsyncIterator@@ -255,20 +256,20 @@ class OpenAIProvider(BaseProvider):)if self._cost_tracker is not None:- import asyncio-- try:- asyncio.get_event_loop().create_task(- self._cost_tracker.record(- model=self._model,- input_tokens=result.input_tokens,- output_tokens=result.output_tokens,- operation="doc_generation",- file_path=None,- )+ # Await the cost record inline rather than spawning a detached+ # task. A fire-and-forget create_task can still be flushing its+ # aiosqlite write when the event loop is torn down (e.g. the+ # asyncio.run teardown after doc generation), which surfaces as a+ # noisy "Event loop is closed" worker-thread traceback. record()+ # swallows its own persistence errors, so generation is unaffected.+ with contextlib.suppress(Exception):+ await self._cost_tracker.record(+ model=self._model,+ input_tokens=result.input_tokens,+ output_tokens=result.output_tokens,+ operation="doc_generation",+ file_path=None,)- except RuntimeError:- pass # No running event loop — skip async recordreturn result