Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Celery leaking state between class based tasks #8972

Open
12 of 19 tasks
agronick opened this issue Apr 17, 2024 · 0 comments
Open
12 of 19 tasks

Celery leaking state between class based tasks #8972

agronick opened this issue Apr 17, 2024 · 0 comments

Comments

@agronick
Copy link

agronick commented Apr 17, 2024

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

  • None

Possible Duplicates

  • None

Environment & Settings

5.3.6 (emerald-rush)
Celery version:

software -> celery:5.3.6 (emerald-rush) kombu:5.3.5 py:3.11.8
            billiard:4.2.0 redis:5.0.3
platform -> system:Linux arch:64bit, ELF
            kernel version:5.15.146.1-microsoft-standard-WSL2 imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:redis results:django-db
celery report Output:

Steps to Reproduce

Required Dependencies

  • Minimal Python Version: 3.11.8
  • Minimal Celery Version: 5.3.6
  • Minimal Kombu Version: 5.3.5
  • Minimal Broker Version: Unknown
  • Minimal Result Backend Version: 2.5.1
  • Minimal OS and/or Kernel Version: N/A
  • Minimal Broker Client Version: N/A
  • Minimal Result Backend Client Version: 2.5.1

Python Packages

pip freeze Output:

asgiref==3.7.2
azure-core==1.30.0
azure-storage-queue==12.9.0
billiard==4.2.0
celery[redis]==5.3.6
certifi==2024.2.2
cffi==1.16.0
cfgv==3.4.0
channels==4.0.0
channels-redis==4.2.0
daphne==4.1.0
charset-normalizer==3.3.2
click==8.1.7
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.3.0
colorama==0.4.6
contourpy==1.2.0
coreapi==2.3.3
coreschema==0.0.4
croniter==2.0.1
cryptography==42.0.4
cycler==0.12.1
distlib==0.3.8
Django==5.0.2
django-celery-results==2.5.1
django-celery-beat==2.6.0
django-cors-headers==4.3.1
django-extensions==3.2.3
django-csp==3.7
django-filter==23.5
django-oauth-toolkit==2.3.0
django-rest-knox==4.2.0
django-rest-swagger==2.2.0
django-users-api==0.1
djangorestframework==3.14.0
django-generate-series==0.4.1
dnspython==2.6.1
drf-jwt==1.19.2
drf-yasg==1.21.7
et-xmlfile==1.1.0
eventlet==0.35.2
filelock==3.13.1
fonttools==4.49.0
greenlet==3.0.3
grpcio==1.62.0
grpcio-tools==1.62.0
identify==2.5.35
idna==3.6
inflection==0.5.1
isodate==0.6.1
itypes==1.2.0
Jinja2==3.1.3
jwcrypto==1.5.4
kiwisolver==1.4.5
kombu==5.3.5
MarkupSafe==2.1.5
matplotlib==3.8.3
nodeenv==1.8.0
numpy==1.26.4
oauthlib==3.2.2
openapi-codec==1.3.2
openpyxl==3.1.2
packaging==23.2
paho-mqtt==2.0.0
pandas==2.2.1
pillow==10.2.0
platformdirs==4.2.0
pre-commit==3.6.2
prompt-toolkit==3.0.43
protobuf==4.25.3
psutil==5.9.8
psycopg2-binary==2.9.9
pycparser==2.21
PyJWT==2.8.0
pyparsing==3.1.1
python-dateutil==2.8.2
python-dotenv==1.0.1
pytz==2024.1
PyYAML==6.0.1
requests==2.31.0
sentry-sdk==1.40.5
sentry-asgi==0.2.0
simplejson==3.19.2
six==1.16.0
sqlparse==0.4.4
typing_extensions==4.9.0
tzdata==2024.1
tzlocal==5.2
uritemplate==4.1.1
urllib3==2.2.1
vine==5.1.0
virtualenv==20.25.1
wcwidth==0.2.13
uvicorn[standard]==0.27.1
xlrd==2.0.1
XlsxWriter==3.2.0
xmltodict==0.13.0

Other Dependencies

N/A

Minimally Reproducible Test Case

class MyTask(celery.Task):

    def __init__(self):
        self.my_list = []

    def run(self, *args, **kwargs):
        self.my_list.append(1)
        print(self.my_list)
        assert len(self.my_list) == 1 # Fails

my_task = app.register_task(MyTask)
my_task.delay()
my_task.delay()

Expected Behavior

The task should be instantiated for each run. State should not be leaking between tasks.

Actual Behavior

I have a list defined in my __init__ method of my task. As the task runs things are appended to the list. I'm noticing that the class is only instantiated once. The list attached to self seems to persist between jobs. This seems very unintuitive and seems like it could lead to concurrency issues with a thread based worker.

Created this post and got no replies: #8958

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

No branches or pull requests

1 participant