Skip to content

fix(jira): keep the transaction usable when two pushes create a ticket for one finding - #17059

Open
Maffooch wants to merge 1 commit into
devfrom
fix/jira-concurrent-create-unique
Open

Maffooch wants to merge 1 commit into
devfrom
fix/jira-concurrent-create-unique

Conversation

@Maffooch

Copy link
Copy Markdown
Contributor

Problem

Two push_finding_to_jira tasks for the same unlinked finding can run at the same time. Both pass the "has no Jira issue" check in push_finding_to_jira, and both call add_jira_issue, so both create a ticket in Jira. The second task to save its JIRA_Issue row 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 atomic block, or a send that holds a transaction), that IntegrityError breaks the transaction. The except block in add_jira_issue then calls failure_to_add_message → log_jira_alert, whose queries raise TransactionManagementError: 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:

psycopg.errors.UniqueViolation: duplicate key value violates unique constraint "dojo_jira_issue_finding_id_key"
  File "dojo/jira/helper.py", in add_jira_issue
    j_issue.save()
...
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.

Fix

In add_jira_issue, save the new JIRA_Issue row inside transaction.atomic() (a savepoint when a transaction is already open), so a failed insert rolls back only that insert.

On IntegrityError:

  • If the object already has a JIRA_Issue row 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.
  • If no such row exists, the error is something else. It is re-raised into the existing except block and reported as a failure as before, but the transaction is still usable.

The unsaved JIRA_Issue that set_obj cached on the object is dropped, so later reads of obj.jira_issue see the saved link.

No migration is needed.

Tests

New JIRAConcurrentCreateTest in unittests/test_jira_helper.py. These DB-backed tests run inside the test case's transaction. A mocked Jira create_issue writes the competing JIRA_Issue row 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 → TransactionManagementError pair seen in production. After the fix, unittests.test_jira_helper, unittests.test_jira_import_and_pushing_api and unittests.test_jira_finding_group_perf all pass (105 tests), and ruff 0.16.10 is clean.

Impact on DefectDojo Pro

  • Pro doesn't override add_jira_issue or push_finding_to_jira.
  • The rules engine Jira sender calls jira_services.push(..., force_sync=True) inside a transaction. It reads the JIRA_Issue row 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.
  • The Go Jira push worker port (dormant) has the same check-then-create shape and may need the same handling when it's enabled.

Related reports

🤖 Generated with Claude Code

…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>
@Maffooch Maffooch added this to the 3.5.400 milestone Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant