Skip to content

The agent that grows with you

fix(cron): remove profile scan ambiguity without dropping long commands

unsupportedpastelsdddad05a473f
diff --git a/cron/lifecycle_guard.py b/cron/lifecycle_guard.py
index f145d4db8d5..51378772ed2 100644
--- a/cron/lifecycle_guard.py
+++ b/cron/lifecycle_guard.py
@@ -246,12 +246,16 @@ _ARGV_LIST_PUNCTUATION = re.compile(r"[\[\],]+")
_PROFILE_FLAG_LIFECYCLE_PATTERN = re.compile(
r"(?i)"
r"hermes\s+"
- # Any global flags before the profile selector (each may carry a value).
- r"(?:-{1,2}\S+(?:\s+(?!-)\S+)?\s+){0,12}"
+ # Each flag has one parse: '-' plus its remainder, never two ways to split
+ # '--'. A following flag cannot also be an optional value (#129281).
+ # Possessive token/space runs avoid repartitioning whitespace on failure;
+ # whole flag groups can still backtrack to expose the profile selector.
+ # Do not cap the number of flags: that would silently allow long self-stops.
+ r"(?:-\S++(?:\s++(?!-\S)\S++)?\s++)*"
# The selector: exactly the shapes the CLI's `_apply_profile_override` accepts.
r"(?:--profile=([^\s]+)|(?:-p|--profile)\s+([^\s]+))"
# Any global flags between the selector and the subcommand.
- r"(?:\s+-{1,2}\S+(?:\s+(?!-)\S+)?){0,12}"
+ r"(?:\s++-\S++(?:\s++(?!-\S)\S++)?)*"
r"\s+gateway\s+(?:restart|stop)"
)
diff --git a/tests/cron/test_lifecycle_guard_profile_scan.py b/tests/cron/test_lifecycle_guard_profile_scan.py
new file mode 100644
index 00000000000..5c5a1a7d804
--- /dev/null
+++ b/tests/cron/test_lifecycle_guard_profile_scan.py
@@ -0,0 +1,58 @@
+"""Profile scanning must terminate without dropping long commands (#129281)."""
+
+import subprocess
+import sys
+from pathlib import Path
+
+import pytest
+
+
[email protected]("profile", ["default", "sibling"])
+def test_long_profile_scan_preserves_targeting(profile):
+ # A process timeout can interrupt a C regex holding the GIL; a thread cannot.
+ script = r'''
+from cron import lifecycle_guard as guard
+
+guard._current_profile_name = lambda: "default"
+profile = PROFILE
+expected = profile == "default"
+for selector in (f"-p {profile}", f"--profile {profile}", f"--profile={profile}"):
+ for before, after in ((58, 0), (0, 58), (58, 58)):
+ command = "hermes " + "--quiet " * before + selector + " " + "--quiet " * after + "gateway stop"
+ assert guard.contains_gateway_lifecycle_command(command) is expected
+ assert guard.contains_gateway_lifecycle_command_or_referenced_script(command) is expected
+ for before, after in (("--usage-file - ", ""), ("", "--usage-file - ")):
+ command = f"hermes {before}{selector} {after}gateway stop"
+ assert guard.contains_gateway_lifecycle_command(command) is expected
+ assert guard.contains_gateway_lifecycle_command_or_referenced_script(command) is expected
+'''.replace("PROFILE", repr(profile))
+ subprocess.run(
+ [sys.executable, "-c", script],
+ cwd=Path(__file__).resolve().parents[2],
+ check=True,
+ capture_output=True,
+ text=True,
+ timeout=10,
+ )
+
+
+def test_nonmatching_flag_runs_finish():
+ script = r'''
+from cron import lifecycle_guard as guard
+
+for command in (
+ "rclone lsf $HOME/.hermes --recursive --files-only " + "--exclude " * 58,
+ "hermes " + "--quiet " * 1000,
+ "hermes -p default " + "--quiet " * 1000 + "gateway status",
+ "hermes " + ("--option" + " " * 100 + "value " ) * 58,
+):
+ assert guard.contains_gateway_lifecycle_command(command) is False
+'''
+ subprocess.run(
+ [sys.executable, "-c", script],
+ cwd=Path(__file__).resolve().parents[2],
+ check=True,
+ capture_output=True,
+ text=True,
+ timeout=10,
+ )