Skip to content

The agent that grows with you

fix: single-owner fd reclamation, shared backoff helper, clean CLI tick failure

kshitij80dc1836c4f4
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 61a04e22f3f..19359ff092f 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -6564,12 +6564,15 @@ def tick(
except OSError:
pass
if _is_fd_exhaustion(exc):
+ # Reclamation is owned by the ticker loop's except handler
+ # (scheduler_provider.py) — it classifies the raised error and
+ # runs _reclaim_fds_best_effort exactly once per failed tick.
+ # Calling it here too would double the gc.collect() pause.
logger.error(
- "Cron tick could not acquire tick lock: %s — attempting fd "
- "reclamation; scheduler will retry on the next tick",
+ "Cron tick could not acquire tick lock: %s — scheduler will "
+ "attempt fd reclamation and retry with backoff",
exc,
)
- _reclaim_fds_best_effort()
else:
logger.error("Cron tick could not acquire tick lock: %s", exc)
raise
diff --git a/cron/scheduler_provider.py b/cron/scheduler_provider.py
index 8b49ea894c0..9e8879128cf 100644
--- a/cron/scheduler_provider.py
+++ b/cron/scheduler_provider.py
@@ -32,6 +32,20 @@ from typing import Any
_EMFILE_BACKOFF_MAX_SECONDS = 15 * 60 # 15 minutes
+def _backoff_wait_seconds(interval: float, consecutive_failures: int) -> float:
+ """Exponential tick backoff shared by both ticker loops (#87644).
+
+ Returns the plain ``interval`` while healthy; doubles per consecutive
+ fd-exhaustion failure, capped at ``_EMFILE_BACKOFF_MAX_SECONDS``.
+ """
+ if consecutive_failures <= 0:
+ return interval
+ return min(
+ interval * (2 ** (consecutive_failures - 1)),
+ _EMFILE_BACKOFF_MAX_SECONDS,
+ )
+
+
class CronScheduler(ABC):
"""Axis-B trigger provider. Decides WHEN a due cron job fires.
@@ -449,13 +463,7 @@ class InProcessCronScheduler(CronScheduler):
if ok:
clear_ticker_error()
consecutive_failures = 0
- wait_seconds = interval
- if consecutive_failures > 0:
- wait_seconds = min(
- interval * (2 ** (consecutive_failures - 1)),
- _EMFILE_BACKOFF_MAX_SECONDS,
- )
- stop_event.wait(wait_seconds)
+ stop_event.wait(_backoff_wait_seconds(interval, consecutive_failures))
def _start_multiplex(
self,
@@ -566,10 +574,4 @@ class InProcessCronScheduler(CronScheduler):
reset_hermes_home_override(home_token)
if ok:
consecutive_failures = 0
- wait_seconds = interval
- if consecutive_failures > 0:
- wait_seconds = min(
- interval * (2 ** (consecutive_failures - 1)),
- _EMFILE_BACKOFF_MAX_SECONDS,
- )
- stop_event.wait(wait_seconds)
+ stop_event.wait(_backoff_wait_seconds(interval, consecutive_failures))
diff --git a/hermes_cli/cron.py b/hermes_cli/cron.py
index 0557c8f0969..9c69eac0b88 100644
--- a/hermes_cli/cron.py
+++ b/hermes_cli/cron.py
@@ -206,7 +206,17 @@ def cron_list(show_all: bool = False):
def cron_tick():
"""Run due jobs once and exit."""
from cron.scheduler import tick
- tick(verbose=True)
+ try:
+ tick(verbose=True)
+ except OSError as exc:
+ # tick() now propagates real lock-acquisition failures (EMFILE,
+ # EACCES on open, ...) instead of swallowing them as contention
+ # (#87644). For the one-shot CLI surface, report cleanly instead of
+ # dumping a traceback; the gateway ticker loop handles its own retry.
+ print(color(f"✗ Cron tick failed: {exc}", Colors.RED))
+ print(" Check `hermes cron status` and the gateway log for details.")
+ return 1
+ return 0
def cron_runs(job_id: Optional[str] = None, limit: int = 20):
@@ -617,8 +627,7 @@ def cron_command(args):
return 0
if subcmd == "tick":
- cron_tick()
- return 0
+ return cron_tick()
if subcmd in {"runs", "history"}:
cron_runs(getattr(args, "job_id", None), getattr(args, "limit", 20))