diff -r 3b8d92f71d92 -r 3785814bc2b7 mercurial/profiling.py --- a/mercurial/profiling.py Wed Nov 15 22:11:34 2023 +0100 +++ b/mercurial/profiling.py Thu Sep 12 02:24:20 2024 +0200 @@ -10,6 +10,7 @@ import os import signal import subprocess +import sys from .i18n import _ from .pycompat import ( @@ -57,7 +58,23 @@ ) ) p = lsprof.Profiler() - p.enable(subcalls=True) + try: + p.enable(subcalls=True) + except ValueError as exc: + if str(exc) != "Another profiling tool is already active": + raise + if not hasattr(sys, "monitoring"): + raise + # python >=3.12 prevent more than one profiler to run at the same + # time, tries to improve the report to help the user understand + # what is going on. + other_tool_name = sys.monitoring.get_tool(sys.monitoring.PROFILER_ID) + if other_tool_name == "cProfile": + msg = 'cannot recursively call `lsprof`' + raise error.Abort(msg) from None + else: + m = 'failed to start "lsprofile"; another profiler already running: %s' + raise error.Abort(_(m) % other_tool_name) from None try: yield finally: