Skip to content

Resource tracker warning (leaked semaphores) with MCPServerStdio #618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
al0ne opened this issue Apr 28, 2025 · 0 comments
Open

Resource tracker warning (leaked semaphores) with MCPServerStdio #618

al0ne opened this issue Apr 28, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@al0ne
Copy link

al0ne commented Apr 28, 2025

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(
@al0ne al0ne added the bug Something isn't working label Apr 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant