Skip to content

Resource tracker warning (leaked semaphores) with MCPServerStdio #618

Open
@al0ne

Description

@al0ne

Debug information

  • Agents SDK version: 0.0.12
  • Python 3.13.1

Describe the bug

When using an external MCP plugin with the OpenAI Agents SDK (MCPServerStdio in this case), I consistently receive a resource_tracker: There appear to be XX leaked semaphore objects to clean up at shutdown warning message during the program's shutdown. The exact number of leaked objects (e.g., 24) varies.

Steps to reproduce

Use the following code snippet which initializes a custom OpenAI client and then attempts to run an agent configured with an MCPServerStdio plugin.

Code Snippet

import asyncio
from agents import Agent, Runner
from agents import set_default_openai_client, set_default_openai_api, set_tracing_disabled
from agents.mcp import MCPServerStdio
from openai import AsyncOpenAI

# --- Replace with your actual values ---
OPENAI_BASE_URL = "YOUR_CUSTOM_BASE_URL" # e.g., "http://localhost:11434/v1" or similar
OPENAI_API_KEY = "YOUR_API_KEY" # Can be "ollama" or similar if using local endpoint
MODEL_NAME = "YOUR_MODEL_NAME" # e.g., "llama3" or "gpt-4o"
# --- End of replacements ---

async def main():
    # Create custom OpenAI client
    custom_client = AsyncOpenAI(base_url=OPENAI_BASE_URL, api_key=OPENAI_API_KEY)

    # Required configuration for using a custom OpenAI API endpoint
    set_default_openai_api("chat_completions") # Or other API type if needed
    set_default_openai_client(custom_client)
    set_tracing_disabled(True) # Disable tracing if not needed

    # Get Python executable path
    current_dir = os.path.dirname(os.path.abspath(__file__))
    python_executable = sys.executable
    soc_push_script = os.path.join(current_dir, "plugins", "soc_push_script.py") # Assuming this script exists and works as an MCP plugin

    # Initialize the MCP server plugin using MCPServerStdio
    # Make sure soc_push_script.py implements the MCP protocol correctly
    mcp_soc_push = MCPServerStdio(
        name="soc_push_script",
        params={
            "command": python_executable,
            "args": [soc_push_script],
        })

    # Run the agent with the MCP plugin
    try:
        async with mcp_soc_push as push_tool: # Use the plugin as a tool
            agent = Agent(
                name="test",
                instructions='test',
                model=MODEL_NAME,
                mcp_servers=[push_tool], # Add the MCP tool to the agent's servers
            )
            print("Starting agent run...")
            result = await Runner.run(starting_agent=agent, input='')
            print("Agent run finished.")
            print("Final Output:", result.final_output)
    finally:
         # Explicitly attempt cleanup if necessary, though the warning suggests leakage
         print("Attempting cleanup (may not prevent warning)...")
         # Specific MCP cleanup methods might be needed depending on implementation
         pass # Add cleanup logic here if known

# Assuming this script is run directly
if __name__ == "__main__":
    asyncio.run(main())

Note: The soc_push_script.py file is not provided, but it is assumed to be a functional MCP plugin script that interacts via standard I/O.

Console Output

/usr/local/Cellar/python@3.13/3.13.1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/multiprocessing/resource_tracker.py:276: UserWarning: resource_tracker: There appear to be 24 leaked semaphore objects to clean up at shutdown: {'/mp-t9hi4oa_', '/mp-pbo_pje7', '/mp-_1jipfl5', '/mp-04d4uokn', '/mp-jw_gfhjk', '/mp-8122t3qm', '/mp-tjg6llji', '/mp-75j5swhj', '/mp-oprtjvq9', '/mp-_9c_6tt4', '/mp-o6ld1gx6', '/mp-vfh4ncjt', '/mp-6pf__aj0', '/mp-h8x7h4uu', '/mp-_heqxtvk', '/mp-uvzgo0ft', '/mp-gqm7a9dj', '/mp-j09dabgf', '/mp-w30c1pxu', '/mp-4gzlcmxm', '/mp-b5004bcn', '/mp-d1l4chll', '/mp-we2hfv61', '/mp-t1hr16ma'}
  warnings.warn(

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