Skip to content

fix: keep stored task states when tracking starts #283

Closed

Description

@uniqueg

Problem

When the Celery task that tracks a task starts, it sets the state to INITIALIZING unconditionally (pro_tes/tasks/track_task_progress.py, db_client.update_task_state(state=TesState.INITIALIZING.value)). The state reported by the backend is only stored after the first poll in the loop, polling.wait seconds later; the get_task before the loop doesn't store it.

With #275, new tasks are stored with the state that their backend reports (e.g., QUEUED or RUNNING), and tasks can be canceled right after creation (CANCELED), before a worker picks up the tracking job. The tracker then overwrites these states:

  • For polling.wait seconds (or longer while the workers are busy), GET /tasks/{id} shows INITIALIZING for a task that is running, or for one that was canceled.
  • A second cancel request in that window passes the check and cancels the task at the backend again.
  • If the backend doesn't report CANCELED afterwards (e.g., its cancel was a no-op, or tracking ends with SYSTEM_ERROR), the cancel is lost.

Before #275, the overwrite only replaced UNKNOWN, so it did no harm.

Found in the review of the fix for #275.

Proposed change

  • Set INITIALIZING only on tasks that are still UNKNOWN: DbDocumentConnector.update_task_state(..., if_state="UNKNOWN"), added in fix: store and cancel new tasks at backends #275. Or drop the write.
  • Store the state of the get_task before the loop.
  • Never move tasks out of a final state, such as CANCELED.
  • Handle states that the tracker doesn't know (e.g., a backend serving DONE): update_task_state raises ValueError, so tracking ends and the task keeps its last state, e.g., INITIALIZING. Since fix: submit tasks to backends exactly once #274, such tasks are stored with their backend (instead of a 500 that orphaned them), so the tracker should keep polling them, or log them and keep them cancelable (Sourcery review of fix: submit tasks to backends exactly once #282).

This belongs to the redesign of task tracking (findings 3 to 5 of the pre-release review), which changes track_task_progress.py.

Activity

  1. added
    priority: high
    status: triage
    type: bug
    workload: hours
    on Oct 24, 2026
  2. uniqueg commented on Oct 24, 2026

    @uniqueg
    MemberAuthor

    Done in #288. Checked against dev (28a49c6):

    • No INITIALIZING overwrite: the tracker no longer writes INITIALIZING at all. Each run of tasks.track_run_progress polls once and stores the backend's state if it differs from the stored one, so the first get_task is stored too.
    • Final states: a stored CANCELED is kept until the backend reports a finished state (_store_state(..., keep_canceled=True), a conditional update that also covers a cancel committed while a poll runs). Only then is the backend's final state stored, e.g., CANCELED, or COMPLETE if the cancel came too late. Tasks stored in other final states are not tracked: tracking ends once a task is finished, and resumed tracking only picks up unfinished tasks.
    • States the tracker doesn't know (e.g., DONE): py-tes rejects them in get_task (ValueError, raised by its attrs validator), so the poll counts as a failed poll. Tracking goes on: after polling.attempts consecutive failures the task becomes UNKNOWN (cancelable since fix: store and cancel new tasks at backends #291), and polls back off to polling.max_wait. Unknown states are never stored.
  3. removed
    status: triage
    on Oct 24, 2026
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

    priority: hightype: bugworkload: hours

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions