dataagent/core/context/context.py:74-76 的 ContextFactory._instances / _n_instances 是类级全局字典,跨进程/测试不隔离:
dataagent/core/context/context.py:74-76
ContextFactory._instances
_n_instances
class ContextFactory: _instances: dict[tuple, Context] = {} _n_instances: int = 0 _lock = threading.Lock()
sub_agent_entry.py
_instances
sub_id
clear_context()
tests/ut/test_planner_prompt_builder.py
tests/ut/flex/hooks/test_context_reference_rewriter.py
autouse
teardown_method
任选其一或组合:
atexit
contextvars.ContextVar
WeakValueDictionary
@pytest.fixture(autouse=True) def _clear_context_factory(): ContextFactory.clear_context() yield ContextFactory.clear_context()
tests/ut/flex/hooks/test_context_reference_rewriter.py:273
TestContextReferenceRewriterHelpers
major(跨进程/测试状态泄漏)
0623
c5c899a4196717d1eb5944a9736f766310655ef7
现象
dataagent/core/context/context.py:74-76的ContextFactory._instances/_n_instances是类级全局字典,跨进程/测试不隔离:class ContextFactory: _instances: dict[tuple, Context] = {} _n_instances: int = 0 _lock = threading.Lock()影响
sub_agent_entry.py)无法继承父进程的_instances,每个进程独立计数,sub_id从 0 开始可能与父进程冲突clear_context()会导致实例泄漏,后续测试读到上一轮残留状态tests/ut/test_planner_prompt_builder.py与tests/ut/flex/hooks/test_context_reference_rewriter.py已发现多处缺autousefixture /teardown_method清理修复方案
任选其一或组合:
atexit清理:进程退出时调clear_context()contextvars.ContextVar隔离(每个 asyncio task / 线程独立 ContextFactory)WeakValueDictionary让 GC 回收无引用的 Contextautousefixture:@pytest.fixture(autouse=True) def _clear_context_factory(): ContextFactory.clear_context() yield ContextFactory.clear_context()涉及文件
dataagent/core/context/context.py:74-76tests/ut/test_planner_prompt_builder.py(缺 autouse fixture)tests/ut/flex/hooks/test_context_reference_rewriter.py:273(TestContextReferenceRewriterHelpers缺 teardown_method)验证
严重性
major(跨进程/测试状态泄漏)
环境
0623c5c899a4196717d1eb5944a9736f766310655ef7