diff --git a/lib/Projectrans/Phase2Translator.py b/lib/Projectrans/Phase2Translator.py index 4a7bf45..355d4af 100644 --- a/lib/Projectrans/Phase2Translator.py +++ b/lib/Projectrans/Phase2Translator.py @@ -288,14 +288,18 @@ def _ProfileWorker(_fn): def _wrapper(*args, **kwargs): _prof = None try: - # 互斥:viztracer 等外部 profiler 已激活(sys.getprofile() 非 None)时 - # 跳过 cProfile——Python 只允许一个 profiling tool 同时活跃, - # cProfile.enable() 会抛 "Another profiling tool is already active" - # → worker 崩溃 → batch 异常 → 编译终止(viztracer --log_multiprocess 场景)。 - # 此时让 viztracer 独占记录,正常编译(无外部 profiler)仍保留 cProfile。 - if sys.getprofile() is None: + # cProfile 默认不启用(环境变量 TPC_WORKER_PROF=1 开启): + # Python 只允许一个 profiling tool 同时活跃,TPC 的 cProfile 与 + # viztracer --log_multiprocess 冲突(cProfile.enable() 检测到已激活 + # profiler 抛 "Another profiling tool is already active" → worker 崩溃 + # → batch 异常 → 编译终止)。外部 profiler 活跃(sys.getprofile() + # 非 None)时即使开启也跳过让位;enable() 异常兜底吞掉保编译。 + if os.environ.get('TPC_WORKER_PROF', '0') == '1' and sys.getprofile() is None: _prof = cProfile.Profile() - _prof.enable() + try: + _prof.enable() + except RuntimeError: + _prof = None return _fn(*args, **kwargs) finally: if _prof is not None: