From b873f0af6d51001fdc6ce76c3788d9cf524335ee Mon Sep 17 00:00:00 2001 From: Vadym Samoilenko Date: Thu, 7 May 2026 16:45:28 +0100 Subject: [PATCH] fix(translation): use per-language dot-notation to prevent race condition concurrent retranslation tasks (concurrency:2) were each replacing the entire outputs doc, so the last writer silently overwrote the others. Now each task only writes outputs. for the languages it processed. Co-Authored-By: Claude Sonnet 4.6 --- backend/app/tasks/translate_and_synthesize.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/backend/app/tasks/translate_and_synthesize.py b/backend/app/tasks/translate_and_synthesize.py index 98c083d..5d2ab7a 100644 --- a/backend/app/tasks/translate_and_synthesize.py +++ b/backend/app/tasks/translate_and_synthesize.py @@ -280,14 +280,21 @@ async def _async_translate_and_synthesize(job_id: str, languages: list[str] | No finally: pass - # Update status to TTS generating + # Update status to TTS generating. + # Use per-language dot-notation so concurrent single-language retranslation tasks + # don't overwrite each other's results (concurrency:2 race condition). + per_lang_updates = { + f"outputs.{lang}": updated_outputs[lang] + for lang in target_languages + if lang in updated_outputs + } await db.jobs.update_one( {"_id": job_id}, { "$set": { "status": JobStatus.TTS_GENERATING.value, - "outputs": updated_outputs, - "updated_at": datetime.utcnow() + "updated_at": datetime.utcnow(), + **per_lang_updates, }, "$push": { "review.history": {