Skip to content

feat(task): let wait_result retry on result backend errors - #731

Open
Devpaul-01 wants to merge 2 commits into
taskiq-python:masterfrom
Devpaul-01:fix/wait-result-retry-backend-errors
Open

Devpaul-01 wants to merge 2 commits into
taskiq-python:masterfrom
Devpaul-01:fix/wait-result-retry-backend-errors

Conversation

@Devpaul-01

Copy link
Copy Markdown

Fixes #655

Problem

AsyncTaskiqTask.wait_result() polls is_ready() and then calls get_result(). Any ResultBackendError from either call (a Redis read timeout, a connection reset, a failover) propagates straight out of wait_result(), even when the timeout budget has barely been used. The error says nothing about the task itself, which is usually still running or has just finished, so callers see a failure for work that succeeds.

Root cause

wait_result() has no distinction between "the task isn't ready" and "I couldn't ask the backend". Both is_ready() and get_result() re-raise as ResultIsReadyError / ResultGetError, and the polling loop doesn't catch them.

Solution

Add an opt-in retry_on_backend_error: bool = False argument to wait_result(). The default keeps today's fail-fast behaviour, so this is backwards compatible. When enabled, a ResultBackendError while checking readiness or fetching the result is treated as "not ready yet":

  • the poll is retried with exponential backoff, capped at 5s and never below check_interval, so a struggling backend isn't polled at full speed;
  • the backoff delay is clipped so it never sleeps past the timeout deadline;
  • on expiry TaskiqResultTimeoutError is raised from the last backend error, so the underlying failure isn't lost;
  • a successful "not ready" poll resets the delay to check_interval.

The change is split in two commits: readiness polling first, then get_result(), so the second can be dropped if you'd rather not retry reads.

Tests / verification

  • Added unit tests using a small flaky AsyncResultBackend that fails the first N calls: default fail-fast (both is_ready and get_result), retry then success (both paths), timeout with the last error chained as __cause__, exact backoff sequence, and backoff not overshooting the timeout.
  • The new retry tests fail on the current master (the argument doesn't exist) and pass with the change; the fail-fast tests pass on both.
  • Full suite: pytest tests (409 passed). ruff check, ruff format --check and mypy are clean on the touched files.
  • These tests use a fake backend. I have not run this against a live Redis/AMQP backend.

Open question

With timeout=-1 (no timeout) and the flag enabled, a permanent get_result() error (e.g. a corrupt payload) would be retried forever. That's documented in the docstring since an unbounded wait is already the caller's choice, but if you'd prefer a cap on consecutive failures, or want the second commit dropped, happy to adjust.

wait_result() ends the wait on the first ResultIsReadyError, even when
the timeout budget is almost untouched. A single Redis read timeout or
connection reset says nothing about the task, but callers get a failure
for a task that is probably still running or has just finished.

Add an opt-in `retry_on_backend_error` argument (default False, so the
current fail-fast behaviour is unchanged). When enabled, a failed
readiness poll is treated as "not ready yet":

- the poll is retried with an exponentially increasing delay, capped at
  5s (never below check_interval), so a struggling backend isn't hit at
  full polling speed;
- the delay never extends past the timeout deadline;
- on expiry TaskiqResultTimeoutError is raised with the last backend
  error as its __cause__.

Refs taskiq-python#655
A backend blip in the window between "ready" and the actual read
raises ResultGetError out of wait_result(), with the same effect as a
failed readiness poll: the caller sees a failure for a task that
finished fine.

When `retry_on_backend_error` is enabled, treat any ResultBackendError
raised while checking readiness or fetching the result the same way:
retry with backoff until the timeout and chain the last error to
TaskiqResultTimeoutError.

Refs taskiq-python#655

This branch has not been deployed

No deployments
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.

wait_result aborts on a transient result-backend error instead of retrying the poll

1 participant