From a2b1985382ea7964fd0fc6551ef7d7f68ace4e6a Mon Sep 17 00:00:00 2001 From: Evan Lohn Date: Thu, 21 Aug 2025 18:32:02 -0700 Subject: [PATCH] ensure multi-tenant contextvar is passed --- .../background/celery/tasks/docprocessing/heartbeat.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/backend/onyx/background/celery/tasks/docprocessing/heartbeat.py b/backend/onyx/background/celery/tasks/docprocessing/heartbeat.py index 5c3ff1cd6e0..321c1949be9 100644 --- a/backend/onyx/background/celery/tasks/docprocessing/heartbeat.py +++ b/backend/onyx/background/celery/tasks/docprocessing/heartbeat.py @@ -1,3 +1,4 @@ +import contextvars import threading from sqlalchemy import update @@ -24,17 +25,15 @@ def heartbeat_loop() -> None: .values(heartbeat_counter=IndexAttempt.heartbeat_counter + 1) ) db_session.commit() - logger.debug( - "Updated heartbeat counter for index attempt %s", - index_attempt_id, - ) except Exception: logger.exception( "Failed to update heartbeat counter for index attempt %s", index_attempt_id, ) - thread = threading.Thread(target=heartbeat_loop, daemon=True) + # Ensure contextvars from the outer context are available in the thread + context = contextvars.copy_context() + thread = threading.Thread(target=context.run, args=(heartbeat_loop,), daemon=True) thread.start() return thread, stop_event