Skip to content

Recover deadline callbacks whose token was used but that never started #76735

Open

Description

@seanghaeli

This issue spawned out of the review discussion on #76571 (comment)

Since #71192, a worker swaps a deadline callback's single-use token for an execution token with PATCH /execution/callbacks/{id}/run before it runs the callback. The server moves the callback from QUEUED to RUNNING on the first exchange it handles. Two cases can leave a callback that never runs:

  1. Lost response. The server handled the exchange, but the reply never reached the worker (connection dropped, a proxy 502 or 504, a read timeout). The retry gets 409 and the callback fails without running. Don't fail deadline callbacks when the Execution API is briefly slow or down #76571 makes this rarer for 3.4.1 with a longer timeout and retry budget, but doesn't remove it.
  2. Worker death. The worker dies after the exchange. A redelivered message gets 409 and fails, or, if nothing reports back, the callback stays RUNNING, since on main a callback only leaves RUNNING through an executor event.

Proposed fix

The guarantee is at-least-once: for a deadline callback, a missed run is the bug and a duplicate is noise.

  • Every run gets an attempt_id (UUID), written in the same statement as QUEUED → RUNNING in the exchange and carried in the execution token. Later transitions compare on state and attempt_id together.
  • The worker reports when the callback starts, without waiting for the acknowledgement before running it.
  • The scheduler re-dispatches a callback that is RUNNING with no start report after a timeout, with a new attempt_id and token (the old token stops working), up to a cap, then marks it failed.
  • The callback can see its attempt_id, so a callback with side effects can dedupe on it.

This covers both cases above, including a callback stuck in RUNNING.

The per-attempt nonce @uranusjr suggested in the #76571 review is the other half: the worker sends a random nonce with each attempt of the exchange, the server stores its hash with the attempt_id, and re-issues an execution token only for a matching nonce. A copy of the broker message doesn't have the nonce, so the token stays single-use, and a lost response or a proxy 502/503 recovers right away instead of after the timeout. It can land in the same PR or its own.

Needs a new Execution API version.

Open details

  • Workers on an older Execution API version don't report start, so the scheduler should only re-dispatch callbacks taken by a worker that does.
  • Defaults for the timeout and the cap, and whether they're configurable.
  • The context key for attempt_id.

Activity

  1. how2how2how2-arch commented on Nov 9, 2026

    @how2how2how2-arch

    The nonce as described covers the case where the response is lost and the same worker retries, and I think it is worth being explicit about the one it does not cover, because the two are easy to read as one. If the worker dies after the server handled the exchange, the successor takes the message off the broker without a nonce, gets 409, and the callback is marked failed without running. That is unchanged by this proposal rather than made worse, but it means "safe to retry" holds only for the process that already exchanged.

    Whether you want that second cell decides where the nonce lives. Kept in the worker, the coverage is one process. Written to the callback row in the same transaction as QUEUED to RUNNING, a successor can present it, and the exchange becomes idempotent per callback run instead of per process. That seems to cost nothing in what you are defending: what stops a leaked broker copy is that the nonce is absent from the message, not that it is absent from the database.

    One thing I could not answer from the issue: a callback that is RUNNING with the exchange done and no worker left to run it. The only timeout I found is [callbacks] callback_execution_timeout, which reads to me as supervising the callback subprocess from the client side, so it would not fire when there is no client. If nothing moves that row to a terminal state, this proposal shifts the failure from "marked failed without running", which is visible, to a row that stays RUNNING, which is not — the worse of the two. I read that off ACTIVE_STATES including RUNNING while TERMINAL_STATES is SUCCESS and FAILED, so a sweep living outside models/callback.py is easy for me to have missed; if there is one, ignore that paragraph.

  2. seanghaeli commented on Nov 9, 2026

    @seanghaeli
    ContributorAuthor

    I've thought about it more and updated the issue body, let me know what you think @how2how2how2-arch ! I think dispatching the callback again with a fresh token is the right general fix.

  3. changed the title [-]Make the callback token exchange safe to retry with an idempotency key[/-] [+]Recover deadline callbacks whose token was used but that never started[/+] on Nov 9, 2026
  4. how2how2how2-arch commented on Nov 10, 2026

    @how2how2how2-arch

    The fresh-token route covers the second case, so the question is what it costs, and the cost sits in one sentence of the body: that the worker runs the callback only after its start report is acknowledged.

    Read the other direction on that sentence. If the worker stays alive and the acknowledgement is what gets lost, the callback does not run. The row then reads as a callback in progress rather than a missed one, which is the same shape as the failure this issue is about: nothing visibly failed and the deadline went unannounced. So that ordering buys never-twice at the price of sometimes-never. Whether that is a good trade depends on what the effect is, and for a deadline callback it is usually a notification, where running twice is noise and running zero times is the bug being fixed. That asymmetry is my argument for making the guarantee at-least-once with an attempt identity the effect can dedupe on, rather than exactly-once: neither case then needs a report round trip sitting in the path.

    If you do want exactly-once, the clock needs an owner, and that is the part the body assumes rather than names. On main nothing outside handle_event writes a terminal state, so a row that was exchanged and never reported started is only detectable by something scanning RUNNING rows that carry no start stamp. That scanner is worth having regardless of which design wins, because it is the only thing that would ever make the stuck row visible at all.

    One note on the row itself: there is no attempt or nonce column today. The closest existing idiom is processor_claim_id, a UUID written alongside the state change, and both designs want exactly that shape. Which means the compare-and-set is one statement over (state, claim id) and not two writes over state; if they are two, the window between them is where a re-dispatch and a start report can both win. Same for the exchange route, where state is the only thing set before the execution token is issued, so the attempt identity has to be written in that same statement or the token and the attempt can be reporting different attempts.

  5. seanghaeli commented on Nov 21, 2026

    @seanghaeli
    ContributorAuthor

    Agreed on at-least-once. For a deadline callback, missing the notification is the bug and a duplicate is noise, so the worker shouldn't wait on the start acknowledgement before running. Agreed too that something has to own the timeout and that the attempt goes in the same statement as the state change. The shape I'd go with:

    • every run gets an attempt_id, written with QUEUED → RUNNING in the exchange and carried in the token, and transitions compare on state and attempt_id together
    • the worker reports start without waiting for the acknowledgement
    • the scheduler re-dispatches a RUNNING callback with no start report after a timeout, with a new attempt and token, up to a cap, then marks it failed
    • the per-attempt nonce stays a possible follow-up for faster recovery from a lost response

    This also covers the lost-response and proxy 502/503 cases @amoghrajesh raised on #76571: the callback is re-dispatched instead of failing. The nonce would make those recover right away instead of after the timeout, so it's welcome in the same PR.

    I'll update the issue body to this. If someone wants to pick it up, go ahead. It needs a new Execution API version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions