Skip to content

fix(tasks): freeze the timeout budget while a task is paused - #2

Merged
BaaRai merged 1 commit into
mainfrom
fix/pause-preserves-timeout-budget
Oct 9, 2026
Merged

BaaRai merged 1 commit into
mainfrom
fix/pause-preserves-timeout-budget

Conversation

@BaaRai

@BaaRai BaaRai commented Oct 9, 2026

Copy link
Copy Markdown
Owner

Fixes #1

What was wrong

deadline_at (= first_run_at + timeout_seconds) is a wall-clock timestamp, and pausing a task did nothing to it. So the timeout kept burning while the task was parked: pause overnight (or shut the machine down while paused, which is the same thing once the deadline is persisted), and the deadline rings while parked. On resume the engine immediately runs the settle sequence and the task ends in timeout with almost no budget actually spent running.

This bit in practice on the very first real run here: a 3h task ran ~2h, was paused overnight, and settled into timeout on resume the next morning.

The archive-restore path already re-based deadlines for paused tasks, so the pause/resume path was inconsistent with it.

The fix

  • ApplyTaskPause stamps paused_at = now() in the same atomic UPDATE that records the user pause
  • ApplyTaskAdmission credits the paused interval back: deadline_at = deadline_at + (now() - paused_at) in the same atomic UPDATE that ends the pause, and clears paused_at. The shifted value flows to the live task handle through the existing RETURNING mirror, so the engine clock context picks it up with no extra plumbing
  • migration is one idempotent ALTER TABLE tasks ADD COLUMN IF NOT EXISTS paused_at TIMESTAMPTZ in schema.sql

Multiple pause/resume cycles each credit their own interval. Tasks paused before the upgrade have paused_at = NULL and keep the old behavior (documented in the commit).

Known limitation, kept out of scope on purpose: time spent waiting in the concurrency queue after resume still counts, exactly like today. Only the paused interval is credited.

Testing

New TestPauseShiftsTaskDeadlineOnResume (server package, DB-backed): stamps a 1h deadline, pauses, waits ~1.1s, admits out of the paused state, and asserts the deadline shifted by at least the paused interval, paused_at was cleared, and the live handle mirrors the shifted deadline.

Full suite with a live Postgres: only pre-existing failure is TestInterceptFilterHTTP, which fails identically on main without this change (verified by re-running it on the stashed base).

go build ./... clean, gofmt clean.

deadline_at is a wall-clock timestamp, so pause time counted against the
task timeout: pausing overnight (or shutting the machine down while
paused) made the deadline ring while parked, and resuming landed
straight into the settle sequence and a timeout terminal state.

ApplyTaskPause now stamps paused_at, and ApplyTaskAdmission credits the
paused interval back onto deadline_at in the same atomic UPDATE that
ends the pause (paused_at is cleared there too). The shifted deadline
flows to the live handle via the existing RETURNING mirror, so the
engine clock context picks it up. Matches the re-basing the
archive-restore path already did for paused tasks.

Migration is an idempotent ADD COLUMN; tasks paused before the upgrade
have no paused_at and keep the old behavior.

Fixes #1
@BaaRai
BaaRai merged commit 52e0bb1 into main Oct 9, 2026
@BaaRai
BaaRai deleted the fix/pause-preserves-timeout-budget branch October 9, 2026 09:46
murilosoave pushed a commit to murilosoave/ARTEX that referenced this pull request Oct 25, 2026
…udget

fix(tasks): freeze the timeout budget while a task is paused
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Task timeout keeps burning while a task is paused (deadline_at is wall-clock)

1 participant