fix: prevent UnboundLocalError and TaskLock race condition in run_dbt_commands - #1404
Open
sentry[bot] wants to merge 1 commit into
Open
fix: prevent UnboundLocalError and TaskLock race condition in run_dbt_commands#1404sentry[bot] wants to merge 1 commit into
sentry[bot] wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1404 +/- ##
==========================================
+ Coverage 59.58% 59.86% +0.28%
==========================================
Files 138 138
Lines 16544 16560 +16
==========================================
+ Hits 9857 9913 +56
+ Misses 6687 6647 -40 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR addresses two related issues in the
run_dbt_commandsCelery task:UnboundLocalError: local variable 'taskprogress' referenced before assignment
UnboundLocalErroroccurred if an exception was raised early in thetryblock (e.g., duringTaskLock.objects.get_or_create) before thetaskprogressvariable was initialized. Theexcepthandler then tried to calltaskprogress.add(), leading to the error.taskprogressis now initialized toNonebefore thetryblock, and its usage in theexcepthandler is guarded with anif taskprogress is not None:check.Race condition in
TaskLock.objects.get_or_createTaskLockmodel has aOneToOneFieldonorgtask, meaningorgtask_idis unique. However,get_or_createwas attempting to look up by(orgtask, locked_by, celery_task_id). If two concurrent tasks for the sameorgtask(but differentcelery_task_ids) ran, thegetpart would fail (due tocelery_task_idmismatch), thecreatepart would then fail with anIntegrityError(due toorgtask_iduniqueness), and the subsequentgetretry would also fail, propagating theIntegrityError.TaskLock.objects.get_or_createto use onlyorgtask=orgtaskfor the lookup, movinglocked_byandcelery_task_idinto thedefaultsdictionary. This ensures the lookup correctly identifies an existing lock by its uniqueorgtask. If a lock already exists, thecelery_task_idandlocked_byfields are explicitly updated to reflect the current task taking ownership.Fixes DALGO-BACKEND-1EQ