4

I am using celery with a fastAPI.

Getting Can't decode message body: ContentDisallowed('Refusing to deserialize untrusted content of type json (application/json)') while running in docker. When running the same in local machine without docker there is not issue.

The configuration for the same is as below.

celery_app = Celery('cda-celery-tasks',
                    broker=CFG.BROKER_URL,
                    backend=CFG.BACKEND_URL,
                    include=['src.tasks.tasks']
                    )

celery_app.conf.task_serializer = 'pickle'
celery_app.conf.result_serializer = 'pickle'
celery_app.conf.accept_content = ['pickle']
celery_app.conf.enable_utc = True

While Running in docker I am getting the error continuously

FROM python:3.8
WORKDIR /app

COPY . .

RUN pip3 install poetry
ENV PATH="/root/.poetry/bin:$PATH"

RUN poetry install

the celery is started using the following command from kubernetes.

poetry run celery -A src.infrastructure.celery_application worker --loglevel=INFO --concurrency 2

While running I am getting the error continuously

Can't decode message body: ContentDisallowed('Refusing to deserialize untrusted content of type json (application/json)')

body: '{"method": "enable_events", "arguments": {}, "destination": null, "pattern": null, "matcher": null}' (99b)
Traceback (most recent call last):
  File "/root/.cache/pypoetry/virtualenvs/cda-9TtSrW0h-py3.8/lib/python3.8/site-packages/kombu/messaging.py", line 620, in _receive_callback
    decoded = None if on_m else message.decode()
  File "/root/.cache/pypoetry/virtualenvs/cda-9TtSrW0h-py3.8/lib/python3.8/site-packages/kombu/message.py", line 194, in decode
    self._decoded_cache = self._decode()
  File "/root/.cache/pypoetry/virtualenvs/cda-9TtSrW0h-py3.8/lib/python3.8/site-packages/kombu/message.py", line 198, in _decode
    return loads(self.body, self.content_type,
  File "/root/.cache/pypoetry/virtualenvs/cda-9TtSrW0h-py3.8/lib/python3.8/site-packages/kombu/serialization.py", line 242, in loads
    raise self._for_untrusted_content(content_type, 'untrusted')
kombu.exceptions.ContentDisallowed: Refusing to deserialize untrusted content of type json (application/json)

Could someone please tell me the possible cause and solution to manage the same? If I've missed anything, over- or under-emphasized a specific point, please let me know in the comments. Thank you so much in advance for your time.

Nithin Varghese
  • 893
  • 1
  • 6
  • 28

1 Answers1

3

Configuring the celery_app with the accept_content type seems to fix the issue:

celery_app.conf.accept_content = ['application/json', 'application/x-python-serialize', 'pickle']

Nithin Varghese
  • 893
  • 1
  • 6
  • 28