mercurial/profiling.py
changeset 51838 3785814bc2b7
parent 51757 812a094a7477
parent 51836 1bb71046f5e0
child 51859 f4733654f144
--- 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: