Skip to content

Fix premature task completion timestamps - #522

Open
sipa-echo-zaoa wants to merge 1 commit into
celery:mainfrom
sipa-echo-zaoa:fix/task-result-completion-time
Open

sipa-echo-zaoa wants to merge 1 commit into
celery:mainfrom
sipa-echo-zaoa:fix/task-result-completion-time

Conversation

@sipa-echo-zaoa

Copy link
Copy Markdown

Task results currently receive a date_done timestamp as soon as they are saved, including when Celery records STARTED or a progress update. A running task therefore appears to have a completion time, and later metadata saves change that time.

Make TaskResult.date_done nullable and set it explicitly when storing SUCCESS, FAILURE, or REVOKED. Pending, running, retrying, and custom progress states keep it unset; returning to an unfinished state clears it. Ordinary model saves preserve the completion timestamp.

The migration clears misleading timestamps on existing unfinished results while preserving completed results. Its reverse fills null timestamps from date_created before restoring the non-null schema.

Cleanup continues to expire completed results by date_done and falls back to date_created for unfinished results, so abandoned rows can still expire. This means an unfinished result older than result_expires can be deleted even while its task is running. The documentation describes this behavior.

Validation:

  • Full suite on Python 3.11, Django 5.2, Celery 5.6, and PostgreSQL 18: 81 passed, 1 skipped.
  • SQLite timestamp, cleanup, and migration checks: 29 passed.
  • Flake8 and git diff --check pass.
  • Regression tests cover initial creation and subsequent updates, terminal and nonterminal states, retries, metadata saves, cleanup, and forward/reverse migration.

Fixes #415.

@auvipy

auvipy commented Nov 15, 2026

Copy link
Copy Markdown
Member

@MehrazRumman

@MehrazRumman MehrazRumman left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read through 3edcb23. Setting date_done only for ready states matches Celery itself: BaseBackend._get_result_meta() sets date_done for READY_STATES and None otherwise. So the meta from this backend would now agree with the other backends, and the migration tests cover both directions. A few things worth looking at before merging:

1. Long-running tasks that report progress can now expire while they run

Before this change, auto_now=True updated date_done on every save, including update_state() progress saves. A task that keeps reporting progress was therefore never older than result_expires. With the new fallback (date_done IS NULL AND date_created < cutoff), such a row is deleted by celery.backend_cleanup one result_expires after it was created, even while it's still running and updating. With the default of one day, any task that runs over a day is affected. When it finishes, store_result() goes through get_or_create() and creates a new row. That loses the original date_created and date_started, and anything that read the progress meta sees the row disappear in the meantime.

The docs note describes this, but I think it's a regression for a fairly common pattern. A few options:

  • Keep a separate "last updated" timestamp (auto_now=True) and expire unfinished rows by it. That keeps the current expiry behaviour and makes date_done correct.
  • At least fall back to date_started before date_created (Coalesce('date_started', 'date_created')). That helps tasks that wait in the queue a long time, but not tasks that run for a long time.
  • Don't expire unfinished rows here at all. That leaves abandoned rows, which is the reason for the fallback, so it's probably the weakest of the three.

2. ordering = ['-date_done'] now puts NULLs in a different place on each database

TaskResult.Meta.ordering and the admin both sort by -date_done. For a descending sort, PostgreSQL and Oracle put NULLs first, while MySQL and SQLite put them last. So on one database every unfinished task is now at the top of the admin list, and on another at the bottom. Their order among themselves isn't defined either. Something like ordering = [F('date_done').desc(nulls_first=True), '-date_created'], in this same migration, would make it the same everywhere. date_hierarchy = 'date_done' and the date_done list filter now also hide every unfinished task once a date is chosen. That can surprise people who use the admin to watch running tasks.

3. Worth a Changelog entry as a backward-incompatible change

date_done used to be always set. Code that does result.date_done - result.date_started, or sorts and filters on date_done expecting every row, will now get None or miss rows. The note in getting_started.rst is good, but the Changelog isn't updated. I think it should be listed under the next release as a behaviour change, together with the expiry change from point 1.

4. Migration cost on large tables

On MySQL/InnoDB, making a column nullable rebuilds the table. The RunPython step also runs an UPDATE ... WHERE NOT (status IN (...)) over the whole table, inside the same migration. Result tables are often large, so a line in the upgrade notes saying the migration can take a while on big tables would help.

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.

date_done does not reflect the time the *task* was completed, but the time the *task result* is updated

3 participants