Skip to content

fix: replace mutable default arguments with None in StatefulToolEnv and MCPEnv#1331

Open
vominh1919 wants to merge 1 commit into
PrimeIntellect-ai:mainfrom
vominh1919:fix/mutable-default-args
Open

fix: replace mutable default arguments with None in StatefulToolEnv and MCPEnv#1331
vominh1919 wants to merge 1 commit into
PrimeIntellect-ai:mainfrom
vominh1919:fix/mutable-default-args

Conversation

@vominh1919
Copy link
Copy Markdown

@vominh1919 vominh1919 commented May 10, 2026

Problem

Two instances of the classic Python mutable default argument bug:

1. StatefulToolEnv.add_tool(args_to_skip=[])stateful_tool_env.py:69

The default [] is created once at function definition time and shared across all calls. If any caller mutates it (e.g., args_to_skip.append(x)), subsequent calls without an explicit argument see the mutated list. This is a well-known Python footgun.

2. MCPEnv.__init__(mcp_servers=[])mcp_env.py:151

Same issue — the default list is shared across all MCPEnv instances that don't pass mcp_servers explicitly.

Fix

Replace =[] with =None and initialize inside the function body:

# Before
def add_tool(self, tool: Callable, args_to_skip: list[str] = []):

# After
def add_tool(self, tool: Callable, args_to_skip: list[str] | None = None):
    if args_to_skip is None:
        args_to_skip = []

Also changed if mcp_servers: to if mcp_servers is not None: to correctly handle an explicitly passed empty list (which is truthy with if mcp_servers: but should still be iterated).

Tests

  • Both files pass ast.parse() syntax check
  • No behavioral change for callers passing explicit arguments
  • Callers passing no argument now get a fresh [] each time instead of a shared one

Note

Low Risk
Low risk: changes only adjust default parameter initialization to prevent shared mutable state; runtime behavior should be unchanged except eliminating cross-call/instance leakage.

Overview
Prevents Python mutable-default-argument bugs in MCPEnv.__init__ and StatefulToolEnv.add_tool by switching list defaults to None and instantiating fresh lists inside the method.

Also updates the MCP server initialization guard to if mcp_servers is not None so an explicitly provided empty list is handled consistently.

Reviewed by Cursor Bugbot for commit 04ad13c. Bugbot is set up for automated code reviews on this repo. Configure here.

…nd MCPEnv

Two instances of the classic Python mutable default argument bug:

1. StatefulToolEnv.add_tool(args_to_skip=[]) — the default [] is created
   once at function definition time and shared across all calls. If any
   caller mutates it (e.g., args_to_skip.append(...)), subsequent calls
   without an explicit argument see the mutated list.

2. MCPEnv.__init__(mcp_servers=[]) — same issue. The default list is
   shared across all MCPEnv instances that don't pass mcp_servers explicitly.

Fix: Replace `=[]` with `=None` and initialize inside the function body.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant