Skip to content

MongoDB and Cassandra backends let a second run of a task overwrite its stored SUCCESS #10929

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 (451381d) and 5.6.3. On 5.5.3, a shorter script that only calls store_result shows the same thing.

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-v140 imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:redis results:mongodb://localhost:27017/celery

broker_url: 'redis://:********@localhost:6379/0'
result_backend: 'mongodb://localhost:27017/celery'
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: 5.5.3 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 (MongoDB 7.0.45 and Cassandra 4.1.12 here)
  • 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 (pymongo 4.18.2, cassandra-driver 3.30.1)

Python Packages

pip freeze Output:

amqp==5.4.1
billiard==4.3.1
cassandra-driver==3.30.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
Deprecated==3.0.0
dnspython==2.9.0
geomet==1.1.0
kombu==5.7.0rc1
packaging==26.3
prompt_toolkit==3.0.53
pymongo==4.18.2
python-dateutil==2.9.0.post0
redis==8.1.0
six==1.17.0
typing_extensions==4.16.0
tzdata==2026.4
tzlocal==5.4.5
vine==5.1.0
wcwidth==0.9.3
wrapt==2.5.1

Other Dependencies

Details

Redis 7.0.15 on localhost:6379 as the broker. Results go to MongoDB 7.0.45 on localhost:27017, or to Cassandra 4.1.12 on localhost:9042 with a keyspace named celery.

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="mongodb://localhost:27017/celery")
# For Cassandra, use these two lines instead:
# app = Celery("tasks", broker="redis://localhost:6379/0", backend="cassandra://")
# app.conf.update(cassandra_servers=["localhost"], cassandra_keyspace="celery", cassandra_table="results")
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

On a new Cassandra keyspace, Celery creates the table on the first write. If run.py reads before that, it fails with "table results does not exist". Delete killed and run it again.

Expected Behavior

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

PENDING -> STARTED -> SUCCESS report-3079

The same script prints this with backend="redis://localhost:6379/1". BaseKeyValueStoreBackend._store_result ignores any update once a task has a SUCCESS.

Actual Behavior

MongoDB:

PENDING -> STARTED -> SUCCESS report-7955 -> STARTED -> SUCCESS report-4462

Cassandra:

PENDING -> STARTED -> SUCCESS report-1087 -> STARTED -> SUCCESS report-3260

After the restart, the task runs again, and the second run writes STARTED and then a new result over the stored SUCCESS. Both backends subclass BaseBackend, not BaseKeyValueStoreBackend, so they don't have its SUCCESS check:

  • MongoBackend._store_result replaces the whole document with replace_one(..., upsert=True).
  • CassandraBackend._store_result inserts a new row, keyed by date_done, and readers get the newest row. The first SUCCESS is still in the table, but nothing reads it.

A fix differs per backend. On MongoDB, filtering the upsert on status != SUCCESS raises DuplicateKeyError when the document already has SUCCESS, so it would have to catch that, or read first. On Cassandra, IF NOT EXISTS doesn't help, since each write is a new row.

Activity

  1. aniketatgithub commented on Oct 24, 2026

    @aniketatgithub
    Contributor

    I'll work on this.

  2. ZedingZhang commented on Oct 24, 2026

    @ZedingZhang
    Contributor

    I’d like to take the Cassandra part of this issue, since #10944 covers MongoDB only. I’ll preserve an existing SUCCESS result and add regression tests, including the first-write/table-creation path.

  3. added this to the 5.7.0 milestone on Oct 26, 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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions