Skip to content

The agent that grows with you

fix(cron): install the fire secret scope before the body's first crash-notice exit

kshitijk4poor9715a7c6fe79
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 5b6d1d75cde..6a55ece1713 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -3344,6 +3344,11 @@ def _run_one_job_body(
_fire_scope_tokens = None
_terminal_scope_token = None
try:
+ # get_secret() fails closed outside a scope; the ticker thread has none. Delivery adapters
+ # resolve credentials, so the scope must span delivery too (reset in the outer finally) —
+ # including the crash notice in the except below when claim_dispatch or
+ # mark_execution_running raises, so install it before either.
+ _fire_scope_tokens = _install_fire_secret_scope()
# Commit a finite one-shot's dispatch BEFORE its side effect so a tick dying mid-run cannot
# re-fire it forever on restart. No-op for recurring/infinite jobs (at-most-times).
# This lives here in the shared body so BOTH the built-in ticker and the external provider (Chronos
@@ -3365,9 +3370,6 @@ def _run_one_job_body(
logger.warning("Cron job %s lost execution ownership before start; skipping", job["id"])
return True
- # get_secret() fails closed outside a scope; the ticker thread has none. Delivery adapters
- # resolve credentials, so the scope must span delivery too (reset in the outer finally).
- _fire_scope_tokens = _install_fire_secret_scope()
# Same for terminal policy (gateway/run.py _profile_runtime_scope): else the ticker reads
# process-global TERMINAL_* env a concurrent profile pinned. Resolution failure installs a
# refusal scope — terminal execution raises instead of using the launch process's policy.
diff --git a/tests/cron/test_restart_safe_worker.py b/tests/cron/test_restart_safe_worker.py
index 2e6c9a984c8..32b3c5c63c9 100644
--- a/tests/cron/test_restart_safe_worker.py
+++ b/tests/cron/test_restart_safe_worker.py
@@ -911,13 +911,15 @@ def test_dispatch_failure_opens_incident_and_delivers_failure_notice(
"suppressed_acked"
[email protected]("failing_stage", ["handoff", "claim_dispatch"])
def test_dispatch_failure_notice_resolves_the_owning_profiles_home_channel(
- execution_ledger, monkeypatch, tmp_path
+ execution_ledger, monkeypatch, tmp_path, failing_stage
):
"""Under multiplex the failure notice for a failed handoff resolves the job's home channel
through ``get_secret``. The in-process run path installs the owning profile's secret scope
before delivery; the dispatch-failure branch must too, or the read fails closed with
- UnscopedSecretError and the notice never leaves."""
+ UnscopedSecretError and the notice never leaves. Same for a crash in the in-process body
+ before the run starts (``claim_dispatch`` raising)."""
import cron.incidents as incidents
import cron.scheduler as scheduler
import cron.scheduler_delivery as delivery
@@ -932,7 +934,11 @@ def test_dispatch_failure_notice_resolves_the_owning_profiles_home_channel(
def _handoff_boom(_job):
raise RuntimeError("worker exited before ownership acknowledgement")
- monkeypatch.setattr(scheduler, "_launch_external_cron_worker", _handoff_boom)
+ if failing_stage == "handoff":
+ monkeypatch.setattr(scheduler, "_launch_external_cron_worker", _handoff_boom)
+ else:
+ monkeypatch.setattr(scheduler, "_launch_external_cron_worker", lambda _job: False)
+ monkeypatch.setattr(scheduler, "claim_dispatch", _handoff_boom)
monkeypatch.setattr(scheduler, "mark_job_run", lambda *_a, **_k: True)
resolved = []
@@ -947,7 +953,7 @@ def test_dispatch_failure_notice_resolves_the_owning_profiles_home_channel(
secret_scope.set_multiplex_active(True)
try:
assert secret_scope.current_secret_scope() is None
- assert scheduler.run_one_job(job, adapters=None) is True
+ assert scheduler.run_one_job(job, adapters=None) is (failing_stage == "handoff")
assert secret_scope.current_secret_scope() is None # scope does not leak past the fire
finally:
secret_scope.set_multiplex_active(False)