Skip to content

DynamoDB backend: pool processes race to create a missing table, and tasks that ran are stored as FAILURE #11087

Done

Description

@ChristianMurphy

When the DynamoDB result table doesn't exist yet, the pool processes of a worker race to create it. The ones that lose fail their first result write, and their tasks are stored as FAILURE although they ran. This is a regression from #7734 (5.3.0): until then the backend tolerated ResourceInUseException here. I'd treat that error as "another process is creating it" and wait for the table, and I can send that PR.

Main (9c2f238), prefork with 4 processes, 20 tasks, a new table for each run Runs with a FAILURE Tasks stored as FAILURE
The table doesn't exist 6 of 12 1 or 2 of 20
20 more tasks on the same table 0 of 12 0
With the change, the table doesn't exist 0 of 12 0

I ran this against the AWS emulator, not against AWS.

Cause and the change I tried

_get_or_create_table() describes the table and creates it when it isn't found (celery/backends/dynamodb.py:217). Each pool process does that on its first write. A second CreateTable for a table that is being created raises ResourceInUseException, which the method lets through. The exception can't be pickled, and the pool then reports MaybeEncodingError for the task.

From 4.1.0 to 5.2.x the method called create_table first and returned describe_table on ResourceInUseException. #7734 turned the order around and dropped that branch. The AWS API reference lists ResourceInUseException for CreateTable when "You attempted to recreate an existing table".

            if error_code == 'ResourceNotFoundException':
                try:
                    table_description = self._client.create_table(**table_schema)
                except ClientError as exc:
                    code = exc.response['Error'].get('Code', 'Unknown')
                    if code != 'ResourceInUseException':
                        raise
                    # Another process is creating the table.
                    self._wait_for_table_status('ACTIVE')
                    return self._client.describe_table(TableName=self.table_name)

Checklist

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.
  • 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.0 (9c2f238)

celery report Output:
software -> celery:5.7.0 (collider) kombu:5.7.0 py:3.14.6
            billiard:4.3.2 redis:8.1.0
platform -> system:Linux arch:64bit, ELF
            kernel version:6.18.44-fc-v378 imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:redis results:dynamodb://@us-east-1/celery-new

broker_url: 'redis://:********@localhost:6379/0'
result_backend: 'dynamodb://@us-east-1/celery-new'
deprecated_settings: None
dynamodb_endpoint_url: 'https://127.0.0.1:9297'

Steps to Reproduce

Required Dependencies

  • Minimal Python Version: N/A or Unknown
  • Minimal Celery Version: 5.3.0 by the code; I tested main only
  • Minimal Kombu Version: N/A or Unknown
  • Minimal Broker Version: Redis 7.4
  • Minimal Result Backend Version: AWS emulator 1.0.322 (DynamoDB)
  • 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:
billiard==4.3.2
celery @ main (9c2f238)
kombu @ main (445027e)
redis==8.1.0
boto3==1.43.126
botocore==1.43.126

Other Dependencies

Services

N/A

Minimally Reproducible Test Case

tasks.py, run.py and output
# tasks.py
import os

from celery import Celery

# TABLE names a table that doesn't exist yet.
app = Celery("tasks", broker="redis://localhost:6379/0",
             backend=f"dynamodb://@us-east-1/{os.environ['TABLE']}")
app.conf.dynamodb_endpoint_url = os.environ["AWS_ENDPOINT_URL"]


@app.task
def add(x, y):
    return x + y
# run.py
import collections
import time

from tasks import add

results = [add.delay(i, i) for i in range(20)]
time.sleep(10)
print(dict(collections.Counter(r.state for r in results)))
failed = [r for r in results if r.state == "FAILURE"]
if failed:
    print(repr(failed[0].result)[:150])
$ export TABLE=celery-new AWS_ENDPOINT_URL=https://127.0.0.1:9297
$ celery -A tasks worker -c 4 -l warning
$ python run.py
{'SUCCESS': 19, 'FAILURE': 1}
MaybeEncodingError('\'PicklingError("Can\\\'t pickle <class \\\'botocore.errorfactory.ResourceNotFoundException\\\'>: it\\\'s not found as botocore.er
$ python run.py
{'SUCCESS': 20}

Expected Behavior

Every process waits for the table and then stores its result.

Actual Behavior

The stored result of the task is MaybeEncodingError. The task ran and returned.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions