diff --git a/src/agents/tracing/provider.py b/src/agents/tracing/provider.py index 9805a0b68..ef0565d4d 100644 --- a/src/agents/tracing/provider.py +++ b/src/agents/tracing/provider.py @@ -14,6 +14,15 @@ from .traces import NoOpTrace, Trace, TraceImpl +def _safe_debug(message: str) -> None: + """Best-effort debug logging that tolerates closed streams during shutdown.""" + try: + logger.debug(message) + except Exception: + # Avoid noisy shutdown errors when the underlying stream is already closed. + return + + class SynchronousMultiTracingProcessor(TracingProcessor): """ Forwards all calls to a list of TracingProcessors, in order of registration. @@ -83,7 +92,7 @@ def shutdown(self) -> None: Called when the application stops. """ for processor in self._processors: - logger.debug(f"Shutting down trace processor {processor}") + _safe_debug(f"Shutting down trace processor {processor}") try: processor.shutdown() except Exception as e: @@ -306,7 +315,7 @@ def shutdown(self) -> None: return try: - logger.debug("Shutting down trace provider") + _safe_debug("Shutting down trace provider") self._multi_processor.shutdown() except Exception as e: logger.error(f"Error shutting down trace provider: {e}")