Repository navigation
Conversation
…t for one finding Two push_finding_to_jira tasks for the same unlinked finding can both pass the "has no Jira issue" check and both create a Jira ticket. The second one to save its JIRA_Issue row hit the unique finding link, which broke the surrounding transaction, so the failure handler's own queries raised TransactionManagementError and the task crashed, leaving an unlinked duplicate ticket in Jira. Save the JIRA_Issue row inside a savepoint. When the save loses to a link that another push already wrote, keep that link, delete the ticket this push just created (best effort), and report success. Any other integrity error is still reported as a failure, now with a usable transaction. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Problem
Two
push_finding_to_jiratasks for the same unlinked finding can run at the same time. Both pass the "has no Jira issue" check inpush_finding_to_jira, and both calladd_jira_issue, so both create a ticket in Jira. The second task to save itsJIRA_Issuerow hits the unique constraint on the finding link (dojo_jira_issue_finding_id_key).When the push runs inside a transaction (the test case's transaction, a caller's
atomicblock, or a send that holds a transaction), thatIntegrityErrorbreaks the transaction. Theexceptblock inadd_jira_issuethen callsfailure_to_add_message→log_jira_alert, whose queries raiseTransactionManagementError: You can't execute queries until the end of the 'atomic' block. The task crashes, and the losing task's Jira ticket stays in Jira with nothing in DefectDojo linked to it.Observed traceback shape:
Fix
In
add_jira_issue, save the newJIRA_Issuerow insidetransaction.atomic()(a savepoint when a transaction is already open), so a failed insert rolls back only that insert.On
IntegrityError:JIRA_Issuerow in the database, another push won the race. Keep that link, delete the ticket this push just created (best effort; a failed delete is logged, not raised), and return success.exceptblock and reported as a failure as before, but the transaction is still usable.The unsaved
JIRA_Issuethatset_objcached on the object is dropped, so later reads ofobj.jira_issuesee the saved link.No migration is needed.
Tests
New
JIRAConcurrentCreateTestinunittests/test_jira_helper.py. These DB-backed tests run inside the test case's transaction. A mocked Jiracreate_issuewrites the competingJIRA_Issuerow before returning, which simulates another worker linking the finding first.test_losing_a_concurrent_create_keeps_the_winning_link_and_the_transaction_usable: the push succeeds, the finding keeps the original link, and queries after the push still work.test_losing_a_concurrent_create_deletes_its_own_duplicate_ticket: the duplicate ticket is deleted.test_failing_to_delete_the_duplicate_ticket_still_succeeds: a Jira error on delete doesn't fail the push.Before the fix, all three failed with the same
IntegrityError→TransactionManagementErrorpair seen in production. After the fix,unittests.test_jira_helper,unittests.test_jira_import_and_pushing_apiandunittests.test_jira_finding_group_perfall pass (105 tests), andruff 0.16.10is clean.Impact on DefectDojo Pro
add_jira_issueorpush_finding_to_jira.jira_services.push(..., force_sync=True)inside a transaction. It reads theJIRA_Issuerow afterwards to decide what to report. With this change, a lost race returns(True, ...)and the winning row is present, so the sender reports the delivery as sent with the existing key. Before, the transaction broke and the send failed.Related reports
🤖 Generated with Claude Code