The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): fail closed when the TERMINAL_CWD lock times out (#79768)
kshitij11ce6419c3ba
diff --git a/cron/scheduler.py b/cron/scheduler.pyindex e48b0606ad0..d922e243276 100644--- a/cron/scheduler.py+++ b/cron/scheduler.py@@ -554,11 +554,36 @@ class _ReadWriteLock:# running cron job. See _ReadWriteLock and run_job for the usage contract._terminal_cwd_lock = _ReadWriteLock()-# Maximum time a cron job waits for the TERMINAL_CWD lock before proceeding-# in degraded mode (without the lock, risking a leaked cwd override). This-# prevents a wedged or extremely long-running workdir job from silently-# parking every concurrently-firing job behind the unbounded acquire (#79768).-_CWD_LOCK_TIMEOUT_SECONDS = 120.0+# Ceiling on how long a cron job waits for the TERMINAL_CWD lock before+# FAILING (fail-closed, #79768). Derived from the cron inactivity limit+# (HERMES_CRON_TIMEOUT, default 600s): a *wedged* lock holder stops touching+# its activity clock, so the inactivity monitor kills it and the lock is+# released within roughly that limit — a waiter that outlasts it plus margin+# is facing a holder even the monitor could not reap, and must not run:+# proceeding without the lock lets the holder's process-global TERMINAL_CWD+# override leak into this job's shell/file/code-exec commands (wrong-directory+# execution — the exact corruption _ReadWriteLock exists to prevent, see+# test_reader_never_observes_writer_override). A *healthy* long-running+# workdir job past the bound also fails its waiters loudly rather than+# corrupting them silently; the failure names the holder pattern so the fix+# (stagger schedules / drop the workdir) is actionable.+_CWD_LOCK_TIMEOUT_FLOOR_SECONDS = 120.0+_CWD_LOCK_TIMEOUT_MARGIN_SECONDS = 60.0+++def _cwd_lock_timeout_seconds() -> float:+ """Bound for the TERMINAL_CWD lock wait: inactivity limit + margin."""+ raw = os.getenv("HERMES_CRON_TIMEOUT", "").strip()+ try:+ inactivity = float(raw) if raw else 600.0+ except (ValueError, TypeError):+ inactivity = 600.0+ if inactivity <= 0: # 0 = unlimited job runtime; keep the wait bounded.+ inactivity = 600.0+ return (+ max(inactivity, _CWD_LOCK_TIMEOUT_FLOOR_SECONDS)+ + _CWD_LOCK_TIMEOUT_MARGIN_SECONDS+ )def _get_parallel_pool(max_workers: Optional[int]) -> concurrent.futures.ThreadPoolExecutor:@@ -3213,24 +3238,11 @@ def run_job(_holds_cwd_write = _job_workdir is not None_cwd_lock_acquired = Trueif _holds_cwd_write:- if not _terminal_cwd_lock.acquire_write(timeout=_CWD_LOCK_TIMEOUT_SECONDS):+ if not _terminal_cwd_lock.acquire_write(timeout=_cwd_lock_timeout_seconds()):_cwd_lock_acquired = False- logger.warning(- "Job '%s': TERMINAL_CWD write-lock timed out after "- "%.0fs — proceeding without serialization (another "- "workdir job may be stuck). The job's cwd override "- "may leak into concurrent jobs (#79768).",- job_name, _CWD_LOCK_TIMEOUT_SECONDS,- )else:- if not _terminal_cwd_lock.acquire_read(timeout=_CWD_LOCK_TIMEOUT_SECONDS):+ if not _terminal_cwd_lock.acquire_read(timeout=_cwd_lock_timeout_seconds()):_cwd_lock_acquired = False- logger.warning(- "Job '%s': TERMINAL_CWD read-lock timed out after "- "%.0fs — a workdir job is likely stuck. Proceeding "- "without serialization (#79768).",- job_name, _CWD_LOCK_TIMEOUT_SECONDS,- )# Everything after the acquire MUST live inside this try, so the finally# below always releases the lock even if the env override or any later@@ -3241,6 +3253,22 @@ def run_job(_cron_session_token = None_non_dispatcher_token = Nonetry:+ if not _cwd_lock_acquired:+ # Fail closed (#79768): running without the lock would let a+ # concurrent workdir job's process-global TERMINAL_CWD override+ # leak into this job's shell/file/code-exec commands — silent+ # wrong-directory execution, the exact corruption the lock+ # exists to prevent. A loud failure is recoverable (next tick /+ # manual rerun); a job that ran in the wrong directory is not.+ raise TimeoutError(+ f"Timed out waiting for the TERMINAL_CWD "+ f"{'write' if _holds_cwd_write else 'read'} lock after "+ f"{_cwd_lock_timeout_seconds():.0f}s — another workdir cron "+ f"job has held it for longer than the cron inactivity limit "+ f"(wedged, or legitimately long-running). Stagger its "+ f"schedule or remove its workdir to unblock this job "+ f"(#79768)."+ )# Scope cron approval policy to this job. Keep the token so the finally# restores the pre-job state instead of pinning an explicit empty value,# which would suppress the legacy os.environ fallback used by standalone