Skip to content

ValueError during agents.tracing shutdown even when tracing is disabled #502

Closed
@cadenzadesigns

Description

@cadenzadesigns

When running tests involving the openai-agents SDK with pytest, even after explicitly disabling tracing using agents.set_tracing_disabled(True) at the start of the test session, ValueError: I/O operation on closed file errors occur during the final process shutdown. These errors originate from agents.tracing.setup.shutdown attempting to log debug messages after standard output/error streams appear to be closed.

This occurs reliably after the pytest summary (e.g., "XXX passed, Y skipped...") is printed, so it doesn't seem to affect test results but creates noisy output in CI/test logs.

It seems set_tracing_disabled(True) prevents trace export but does not prevent the tracing components from initializing or attempting their shutdown logging.

Debug information

  • Agents SDK version: 0.0.9
  • Python version: 3.12.7
  • Operating System: Darwin 24.3.0 arm64 (macOS Sonoma)
  • Other relevant packages:
    • openai: 1.72.0
    • pytest: 8.3.5
    • poetry: 1.8.5

Repro steps

  1. Set up a Python project using Poetry and install openai-agents, openai, and pytest.
  2. Create a test file (tests/test_agent_run.py) that uses agents.Runner.run or run_sync (even a simple "hello world" agent).
  3. Create a root tests/conftest.py with the following hook to disable tracing programmatically:
    # tests/conftest.py
    import pytest
    import logging
    
    def pytest_configure(config):
        """
        Uses the official SDK function to disable tracing globally.
        """
        try:
            from agents import set_tracing_disabled
            set_tracing_disabled(disabled=True)
            print("\nINFO (root conftest): Called set_tracing_disabled(True).") # Optional print for confirmation
        except ImportError:
            print("\nWARNING (root conftest): Could not import set_tracing_disabled from agents.")
        except Exception as e:
            print(f"\nERROR (root conftest): Error calling set_tracing_disabled: {e}")
  4. Run the tests using poetry run pytest tests/.
  5. Observe the output after the pytest summary line.

Expected behavior

When tracing is disabled using set_tracing_disabled(True), no tracing-related shutdown logic should execute, or at least it should not attempt to log messages that cause ValueError after streams are closed. The test run should finish cleanly after the pytest summary.

Actual behavior

The tests pass, but the following errors are printed to stderr after the pytest summary:

--- Logging error ---
Traceback (most recent call last):
File "/path/to/your/python/lib/python3.12/logging/init.py", line 1163, in emit
stream.write(msg + self.terminator)
ValueError: I/O operation on closed file.
Call stack:
File "/path/to/your/venv/lib/python3.12/site-packages/agents/tracing/setup.py", line 205, in shutdown
logger.debug("Shutting down trace provider")
Message: 'Shutting down trace provider'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "/path/to/your/python/lib/python3.12/logging/init.py", line 1163, in emit
stream.write(msg + self.terminator)
ValueError: I/O operation on closed file.
Call stack:
File "/path/to/your/venv/lib/python3.12/site-packages/agents/tracing/setup.py", line 206, in shutdown
self.multi_processor.shutdown()
File "/path/to/your/venv/lib/python3.12/site-packages/agents/tracing/setup.py", line 72, in shutdown
logger.debug(f"Shutting down trace processor {processor}")
Message: 'Shutting down trace processor <agents.tracing.processors.BatchTraceProcessor object at ...>'

Attempts to disable tracing via the OPENAI_AGENTS_DISABLE_TRACING=1 environment variable or RunConfig(tracing_disabled=True) also failed to prevent these specific shutdown errors in the test environment.

Thanks for looking into this!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions