Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

fix(tracing): harden trace() cleanup + cover edge cases (codex review)

xf gongf2413d9ca999
diff --git a/cubepi/tracing/tracer.py b/cubepi/tracing/tracer.py
index 07b959a..c46d674 100644
--- a/cubepi/tracing/tracer.py
+++ b/cubepi/tracing/tracer.py
@@ -209,13 +209,17 @@ class Tracer:
# an async context it's an ``asyncio.Task`` for the
# flush; callers can ``await detach()`` to wait for
# buffered spans to land (codex overall-review MAJOR).
- flush_task = recorder_detach()
- if mcp_tracing is not None and mcp_token is not None:
- try:
- mcp_tracing.unregister_provider(mcp_token)
- except Exception:
- pass
- return flush_task
+ #
+ # Unregister MCP in a ``finally`` so a raising recorder detach
+ # can't leak the provider registration.
+ try:
+ return recorder_detach()
+ finally:
+ if mcp_tracing is not None and mcp_token is not None:
+ try:
+ mcp_tracing.unregister_provider(mcp_token)
+ except Exception:
+ pass
return detach
@@ -439,7 +443,11 @@ async def trace(
def _log_tracing_warning(message: str, exc: BaseException) -> None:
"""Log a swallowed tracing fault via stdlib ``logging`` — cubepi does not
depend on loguru. Hosts that use loguru can intercept stdlib logging to
- route these records through it."""
- logging.getLogger("cubepi.tracing").warning(
- "cubepi tracing: %s", message, exc_info=exc
- )
+ route these records through it. The log call itself is guarded so a raising
+ logging handler can't defeat the best-effort guarantee."""
+ try:
+ logging.getLogger("cubepi.tracing").warning(
+ "cubepi tracing: %s", message, exc_info=exc
+ )
+ except Exception: # noqa: BLE001 — logging must never break the run # pragma: no cover
+ pass
diff --git a/tests/tracing/test_trace_scope.py b/tests/tracing/test_trace_scope.py
index 254247b..70cfda6 100644
--- a/tests/tracing/test_trace_scope.py
+++ b/tests/tracing/test_trace_scope.py
@@ -9,6 +9,9 @@ silent fallback on attach / flush failures.
from __future__ import annotations
+import asyncio
+
+import pytest
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult
from cubepi.agent.agent import Agent
@@ -112,3 +115,94 @@ async def test_trace_swallows_flush_failure(monkeypatch):
# Restore the real force_flush so shutdown() can clean up.
monkeypatch.undo()
await tracer.shutdown()
+
+
+async def test_trace_swallows_detach_failure(monkeypatch):
+ agent, _provider, tracer = _build()
+ try:
+
+ def _raising_detach(): # noqa: ANN202
+ raise RuntimeError("detach boom")
+
+ monkeypatch.setattr(tracer, "attach", lambda _agent: _raising_detach)
+
+ ran = False
+ async with trace(tracer, agent): # must not raise
+ ran = True
+ assert ran, "body must run even when the detach callable itself raises"
+ finally:
+ await tracer.shutdown()
+
+
+async def test_trace_awaits_flush_task_on_exit(monkeypatch):
+ agent, _provider, tracer = _build()
+ flushed = {"done": False}
+
+ async def _flush() -> None:
+ await asyncio.sleep(0)
+ flushed["done"] = True
+
+ def _detach(): # noqa: ANN202
+ return asyncio.get_running_loop().create_task(_flush())
+
+ monkeypatch.setattr(tracer, "attach", lambda _agent: _detach)
+ try:
+ async with trace(tracer, agent):
+ pass
+ assert flushed["done"], "trace must await the flush task before the scope exits"
+ finally:
+ await tracer.shutdown()
+
+
+async def test_trace_preserves_body_exception_when_cleanup_fails(monkeypatch):
+ agent, _provider, tracer = _build()
+
+ class _Sentinel(Exception):
+ pass
+
+ def _raising_detach(): # noqa: ANN202
+ raise RuntimeError("detach boom")
+
+ monkeypatch.setattr(tracer, "attach", lambda _agent: _raising_detach)
+ try:
+ with pytest.raises(_Sentinel):
+ async with trace(tracer, agent):
+ raise _Sentinel("body failed")
+ finally:
+ await tracer.shutdown()
+
+
+async def test_trace_swallows_mcp_unregister_failure(monkeypatch):
+ # Real attach registers the MCP provider; make unregister blow up on detach.
+ # The body must still run and the failure must not propagate.
+ agent, _provider, tracer = _build()
+ try:
+ import cubepi.mcp._tracing as mcp_tracing
+
+ def _boom(_token): # noqa: ANN202
+ raise RuntimeError("unregister boom")
+
+ monkeypatch.setattr(mcp_tracing, "unregister_provider", _boom)
+
+ ran = False
+ async with trace(tracer, agent):
+ ran = True
+ assert ran, "body must run even when MCP unregister fails on detach"
+ finally:
+ await tracer.shutdown()
+
+
+async def test_trace_does_not_shutdown_tracer(monkeypatch):
+ agent, _provider, tracer = _build()
+ calls = {"n": 0}
+
+ async def _spy_shutdown(*_args, **_kwargs) -> None: # noqa: ANN202
+ calls["n"] += 1
+
+ monkeypatch.setattr(tracer, "shutdown", _spy_shutdown)
+ async with trace(tracer, agent):
+ pass
+ assert calls["n"] == 0, "trace must not shut the tracer down — the owner does"
+ # Real cleanup.
+ monkeypatch.undo()
+ await tracer.shutdown()