Fix CANCEL_NEW strategy not cancelling late runs when limit is full#21086
Merged
Fix CANCEL_NEW strategy not cancelling late runs when limit is full#21086
Conversation
Move deployment concurrency enforcement from the MarkLateRuns service into a new orchestration rule (EnforceDeploymentConcurrencyOnLate) that is part of MarkLateRunsPolicy. This keeps the CANCEL_NEW logic in the orchestration layer alongside SecureFlowConcurrencySlots rather than embedding it in the service, and preserves instrumentation via InstrumentFlowRunStateTransitions. Closes #21060 Co-authored-by: Nate Nowack <nate@prefect.io>
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Collaborator
|
what do you think about this @desertaxle? |
haziqishere
pushed a commit
to haziqishere/prefect
that referenced
this pull request
Mar 11, 2026
…refectHQ#21086) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Nate Nowack <nate@prefect.io>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #21060
Supersedes #21068
Summary
The
CANCEL_NEWdeployment concurrency strategy was only enforced bySecureFlowConcurrencySlots, which fires on* → PENDINGtransitions. When a runner/worker is at capacity, scheduled runs never reach PENDING — they stayScheduleduntil theMarkLateRunsservice marks themLate. These runs accumulated in aLatestate instead of being cancelled.This PR adds a new orchestration rule
EnforceDeploymentConcurrencyOnLatetoMarkLateRunsPolicythat interceptsScheduled → Latetransitions and rejects them with aCancelledstate when the deployment usesCANCEL_NEWand the concurrency limit is full.Why an orchestration rule instead of a service-level check
PR #21068 solved this by adding the concurrency check directly into the
MarkLateRunsservice withforce=True. This PR takes a different approach:SecureFlowConcurrencySlotsand the other deployment concurrency rules incore_policy.pyMarkLateRunsPolicyincludesInstrumentFlowRunStateTransitions, so the cancellation is properly tracked. Theforce=Truepath in Fix CANCEL_NEW strategy not cancelling late runs #21068 bypasses all orchestration rules including instrumentation.MarkLateRunsjust proposesLate; the orchestration layer decides what actually happensreject_transition— the same mechanismSecureFlowConcurrencySlotsalready uses for CANCEL_NEW (line 641)Key review points
FROM_STATES = {SCHEDULED},TO_STATES = {SCHEDULED}becauseLateis aSCHEDULEDstate type with name"Late"— same pattern asEnsureOnlyScheduledFlowsMarkedLateSecureFlowConcurrencySlots(including theisinstance(concurrency_options, dict)coercion)reject_transitionwith aCancelledstate crosses state types (SCHEDULED → CANCELLED), which works because rejections bypass FROM/TO filtering on subsequent rulesChecklist
<link to issue>"mint.json. (N/A)Devin session | Requested by @zzstoatzz