Skip to content

Database backend lets a second run of a task overwrite its stored SUCCESS #10912

Closed

Description

@ChristianMurphy

Checklist

  • I have verified that the issue exists against the main branch of Celery.
  • This has already been asked to the discussions forum first.
  • I have read the relevant section in the
    contribution guide
    on reporting bugs.
  • I have checked the issues list
    for similar or identical bug reports.
  • I have checked the pull requests list
    for existing proposed fixes.
  • I have checked the commit log
    to find out if the bug was already fixed in the main branch.
  • I have included all related issues and possible duplicate issues
    in this issue (If there are none, check this box anyway).
  • I have tried to reproduce the issue with pytest-celery and added the reproduction script below.

Mandatory Debugging Information

  • I have included the output of celery -A proj report in the issue.
    (if you are not able to do this, then at least specify the Celery
    version affected).
  • I have verified that the issue exists against the main branch of Celery.
  • I have included the contents of pip freeze in the issue.
  • I have included all the versions of all the external dependencies required
    to reproduce this bug.

Optional Debugging Information

  • I have tried reproducing the issue on more than one Python version
    and/or implementation.
  • I have tried reproducing the issue on more than one message broker and/or
    result backend.
  • I have tried reproducing the issue on more than one version of the message
    broker and/or result backend.
  • I have tried reproducing the issue on more than one operating system.
  • I have tried reproducing the issue on more than one workers pool.
  • I have tried reproducing the issue with autoscaling, retries,
    ETA/Countdown & rate limits disabled.
  • I have tried reproducing the issue after downgrading
    and/or upgrading Celery and its dependencies.

Related Issues and Possible Duplicates

Related Issues

Possible Duplicates

  • None

Environment & Settings

Celery version: 5.7.0rc1 (collider). It also happens on main (d6d754e), 5.6.3, and every older release I tried, back to 4.4.7.

celery report Output:

software -> celery:5.7.0rc1 (collider) kombu:5.7.0rc1 py:3.13.16
            billiard:4.3.1 redis:8.1.0
platform -> system:Linux arch:64bit, ELF
            kernel version:6.18.44-fc-v139 imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:redis results:db+sqlite:///results.db

broker_url: 'redis://:********@localhost:6379/0'
result_backend: 'db+sqlite:///results.db'
deprecated_settings: None
task_acks_late: True
task_track_started: True
broker_transport_options: 
 'visibility_timeout': 10}

Steps to Reproduce

Required Dependencies

  • Minimal Python Version: N/A or Unknown
  • Minimal Celery Version: 4.4.7 or older (the oldest I tried)
  • Minimal Kombu Version: N/A or Unknown
  • Minimal Broker Version: N/A or Unknown
  • Minimal Result Backend Version: N/A or Unknown (SQLite here, Postgres 17 and MySQL 8.4 in fault tests)
  • Minimal OS and/or Kernel Version: N/A or Unknown
  • Minimal Broker Client Version: N/A or Unknown
  • Minimal Result Backend Client Version: N/A or Unknown

Python Packages

pip freeze Output:

amqp==5.4.1
billiard==4.3.1
celery==5.7.0rc1
click==8.5.0
click-didyoumean==0.3.1
click-plugins==1.1.1.2
click-repl==0.4.1
kombu==5.7.0rc1
packaging==26.3
prompt-toolkit==3.0.53
python-dateutil==2.9.0.post0
redis==8.1.0
six==1.17.0
sqlalchemy==2.1.5
typing-extensions==4.16.0
tzdata==2026.4
tzlocal==5.4.4
vine==5.1.0
wcwidth==0.9.3

Other Dependencies

Details

Redis 7.0.15 on localhost:6379 as the broker. Results go to SQLite.

Minimally Reproducible Test Case

tasks.py, run.py, and the commands to run them

# tasks.py
import os
import random
import signal
import time

from celery import Celery
from celery.signals import task_success

app = Celery("tasks", broker="redis://localhost:6379/0", backend="db+sqlite:///results.db")
app.conf.update(
    task_acks_late=True,
    task_track_started=True,
    broker_transport_options={"visibility_timeout": 10},  # redeliver after 10 s, not 1 hour
)


@app.task
def report():
    time.sleep(1)
    return f"report-{random.randint(1000, 9999)}"


@task_success.connect
def kill_worker_once(sender=None, **kwargs):
    # The task saved SUCCESS but hasn't acked yet. Kill the worker here, once.
    if not os.path.exists("killed"):
        open("killed", "w").close()
        os.killpg(os.getpgid(os.getppid()), signal.SIGKILL)
# run.py
import time

from tasks import app, report

task_id = report.delay().id
seen = []
for _ in range(60):
    result = app.AsyncResult(task_id)  # a new object each time, so nothing is cached
    state = result.state
    if state == "SUCCESS":
        state += f" {result.result}"
    if not seen or seen[-1] != state:
        seen.append(state)
    time.sleep(0.5)
print(" -> ".join(seen))
celery -A tasks worker -c 3    # terminal 1
python run.py                  # terminal 2
# The worker dies about a second later. Wait 15 seconds, then start it again:
celery -A tasks worker -c 3    # terminal 1

Expected Behavior

Once the task has a SUCCESS, readers keep seeing that result:

PENDING -> STARTED -> SUCCESS report-9160

The same script prints this with backend="redis://localhost:6379/1". The key-value backends ignore any update once a task has a SUCCESS.

Actual Behavior

PENDING -> STARTED -> SUCCESS report-8671 -> STARTED -> SUCCESS report-5803

After the restart, the task runs again. The database backend has no SUCCESS check. The second run writes STARTED over the stored SUCCESS, and then a new result. Any crash between saving a result and the ack can do this under acks_late. In a fault test on Postgres with crashes at that point, 7 of 150 results changed. ready() also goes from True back to False, and GroupResult and chord_unlock rely on it.

I have a patch that keeps the first SUCCESS and locks the row with SELECT ... FOR UPDATE. I can open a PR.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions