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

DAN-554 fixing a race condition in celery task wrapper. could randomly blow u… #2321

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions backend/danswer/background/task_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,16 @@ def wrapped_fn(
kwargs_for_build_name = kwargs or {}
task_name = build_name_fn(*args_for_build_name, **kwargs_for_build_name)
with Session(get_sqlalchemy_engine()) as db_session:
# mark the task as started
# register_task must come before fn = apply_async or else the task
# might run mark_task_start (and crash) before the task row exists
db_task = register_task(task_name, db_session)

task = fn(args, kwargs, *other_args, **other_kwargs)
register_task(task.id, task_name, db_session)

# we update the celery task id for diagnostic purposes
# but it isn't currently used by any code
db_task.task_id = task.id
db_session.commit()

return task

Expand Down
2 changes: 1 addition & 1 deletion backend/danswer/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,7 @@ class TaskQueueState(Base):
__tablename__ = "task_queue_jobs"

id: Mapped[int] = mapped_column(primary_key=True)
# Celery task id
# Celery task id. currently only for readability/diagnostics
task_id: Mapped[str] = mapped_column(String)
# For any job type, this would be the same
task_name: Mapped[str] = mapped_column(String)
Expand Down
3 changes: 1 addition & 2 deletions backend/danswer/db/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ def get_latest_task_by_type(


def register_task(
task_id: str,
task_name: str,
db_session: Session,
) -> TaskQueueState:
new_task = TaskQueueState(
task_id=task_id, task_name=task_name, status=TaskStatus.PENDING
task_id="", task_name=task_name, status=TaskStatus.PENDING
)

db_session.add(new_task)
Expand Down
Loading