From 891d39a9ced63c0a71017d63b301b1c742179a9c Mon Sep 17 00:00:00 2001 From: Davide Silvestri <davide@baserow.io> Date: Wed, 26 Mar 2025 13:22:30 +0100 Subject: [PATCH] Resolve "The local cache is not invalidated properly in celery tasks" --- backend/src/baserow/config/celery.py | 17 ++++++++++++++++- backend/src/baserow/core/jobs/tasks.py | 2 -- ...3528_clear_local_cache_for_celery_tasks.json | 8 ++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 changelog/entries/unreleased/bug/3528_clear_local_cache_for_celery_tasks.json diff --git a/backend/src/baserow/config/celery.py b/backend/src/baserow/config/celery.py index 4f780a994..b5510d5ed 100644 --- a/backend/src/baserow/config/celery.py +++ b/backend/src/baserow/config/celery.py @@ -1,5 +1,20 @@ -from celery import Celery +from celery import Celery, signals + +from baserow.core.cache import local_cache app = Celery("baserow") app.config_from_object("django.conf:settings", namespace="CELERY") app.autodiscover_tasks() + + +def clear_local_cache(*args, **kwargs): + """ + Clear the thread-local cache before and after each Celery task to prevent + data leakage between tasks running on the same worker thread. + """ + + local_cache.clear() + + +signals.task_prerun.connect(clear_local_cache) +signals.task_postrun.connect(clear_local_cache) diff --git a/backend/src/baserow/core/jobs/tasks.py b/backend/src/baserow/core/jobs/tasks.py index d4024a94c..f1bdf2279 100644 --- a/backend/src/baserow/core/jobs/tasks.py +++ b/backend/src/baserow/core/jobs/tasks.py @@ -3,7 +3,6 @@ from datetime import timedelta from django.conf import settings from baserow.config.celery import app -from baserow.core.cache import local_cache from baserow.core.jobs.exceptions import JobCancelled from baserow.core.jobs.registries import job_type_registry from baserow.core.sentry import setup_user_in_sentry @@ -15,7 +14,6 @@ from baserow.core.telemetry.utils import setup_user_in_baggage_and_spans queue="export", soft_time_limit=settings.BASEROW_JOB_SOFT_TIME_LIMIT, ) -@local_cache.context() def run_async_job(self, job_id: int): """Run the job task asynchronously""" diff --git a/changelog/entries/unreleased/bug/3528_clear_local_cache_for_celery_tasks.json b/changelog/entries/unreleased/bug/3528_clear_local_cache_for_celery_tasks.json new file mode 100644 index 000000000..042026649 --- /dev/null +++ b/changelog/entries/unreleased/bug/3528_clear_local_cache_for_celery_tasks.json @@ -0,0 +1,8 @@ +{ + "type": "bug", + "message": "Clear local_cache for celery tasks", + "domain": "core", + "issue_number": 3528, + "bullet_points": [], + "created_at": "2025-03-26" +} \ No newline at end of file