Skip to content

Commit 6b58e78

Browse files
authored
fix: Improve tracing shutdown safety (#2229)
1 parent bc374a0 commit 6b58e78

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/agents/tracing/provider.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
from .traces import NoOpTrace, Trace, TraceImpl
1616

1717

18+
def _safe_debug(message: str) -> None:
19+
"""Best-effort debug logging that tolerates closed streams during shutdown."""
20+
try:
21+
logger.debug(message)
22+
except Exception:
23+
# Avoid noisy shutdown errors when the underlying stream is already closed.
24+
return
25+
26+
1827
class SynchronousMultiTracingProcessor(TracingProcessor):
1928
"""
2029
Forwards all calls to a list of TracingProcessors, in order of registration.
@@ -84,7 +93,7 @@ def shutdown(self) -> None:
8493
Called when the application stops.
8594
"""
8695
for processor in self._processors:
87-
logger.debug(f"Shutting down trace processor {processor}")
96+
_safe_debug(f"Shutting down trace processor {processor}")
8897
try:
8998
processor.shutdown()
9099
except Exception as e:
@@ -315,7 +324,7 @@ def shutdown(self) -> None:
315324
return
316325

317326
try:
318-
logger.debug("Shutting down trace provider")
327+
_safe_debug("Shutting down trace provider")
319328
self._multi_processor.shutdown()
320329
except Exception as e:
321330
logger.error(f"Error shutting down trace provider: {e}")

0 commit comments

Comments
 (0)