From 386382b51f1cfe27e19d9d3e88ba4c808dfb9d2e Mon Sep 17 00:00:00 2001 From: Eric Duong Date: Wed, 20 Nov 2024 11:05:02 -0500 Subject: [PATCH] fix(insights): prevent race condition (#26265) --- posthog/clickhouse/client/async_task_chain.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/posthog/clickhouse/client/async_task_chain.py b/posthog/clickhouse/client/async_task_chain.py index 41c5ee41183..afe886897ca 100644 --- a/posthog/clickhouse/client/async_task_chain.py +++ b/posthog/clickhouse/client/async_task_chain.py @@ -2,6 +2,7 @@ import threading import typing from contextlib import contextmanager from functools import partial +import uuid from celery import chain from celery.canvas import Signature @@ -22,12 +23,15 @@ def kick_off_task( query_status: QueryStatus, task_signature: Signature, ) -> None: - task = task_signature.apply_async() + task_id = str(uuid.uuid4()) + query_status.task_id = task_id + manager.store_query_status(query_status) + task = task_signature.apply_async(task_id=task_id) + # During end-to-end tests, the task is executed synchronously, so we have to refresh the status. if isinstance(task, EagerResult): query_status = manager.get_query_status() - query_status.task_id = task.id - manager.store_query_status(query_status) + manager.store_query_status(query_status) def get_task_chain() -> list[tuple[Signature, "QueryStatusManager", QueryStatus]]: