Skip to content

fix(artifact-bundles): Prevent FK violation in indexing race - #125854

Open
sentry[bot] wants to merge 3 commits into
masterfrom
seer/fix/artifact-bundle-indexing-race
Open

sentry[bot] wants to merge 3 commits into
masterfrom
seer/fix/artifact-bundle-indexing-race

Conversation

@sentry

@sentry sentry Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

This PR addresses an IntegrityError that occurs during artifact bundle indexing (sentry.tasks.assemble.assemble_artifacts).

Root Cause:
A race condition exists where one worker, processing an artifact bundle for indexing via index_urls_in_bundle, can have its associated ArtifactBundle row deleted concurrently by another worker (e.g., through _remove_duplicate_artifact_bundles). If the deletion happens after index_urls_in_bundle has loaded the ArtifactBundle object and prepared its ArtifactBundleIndex entries, but before its transaction commits the bulk_create operation, the database will raise an IntegrityError due to a foreign key constraint violation on sentry_artifactbundleindex.artifact_bundle_id.

Fix:
index_urls_in_bundle is updated to wrap the ArtifactBundleIndex.objects.bulk_create call in a try/except IntegrityError block. If an IntegrityError occurs, it indicates that the ArtifactBundle has been deleted. In this scenario, the function now catches the error, increments a new metric (artifact_bundle_indexing.bundle_deleted_during_indexing) for observability, and returns silently. This is the correct behavior as there is no longer an ArtifactBundle to index.

Test Update:
The existing test test_index_urls_in_bundle_deleted_bundle_is_ignored in tests/sentry/debug_files/test_artifact_bundles.py was updated. Previously, deleting the ArtifactBundle also deleted its associated File, causing the test to fail prematurely with File.DoesNotExist before the IntegrityError could be triggered. The test now correctly simulates the race condition by opening the ArtifactBundleArchive before deleting the ArtifactBundle row, ensuring that the bulk_create call (and thus the IntegrityError handler) is properly exercised.

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

Fixes SENTRY-5HG7

@sentry <feedback>: Autofix iterates on these changes
@sentry stop iterating: Autofix stops iterating on this run

This PR was automatically generated by Sentry. You can adjust this setting at any time.

@sentry
sentry Bot requested a review from a team as a code owner September 29, 2026 10:48
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Sep 29, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 719bd58. Configure here.

@sentry
sentry Bot force-pushed the seer/fix/artifact-bundle-indexing-race branch from 719bd58 to 8b12005 Compare September 29, 2026 10:58
@jjbayer jjbayer added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Sep 29, 2026
@jjbayer
jjbayer enabled auto-merge (squash) September 29, 2026 11:05
@loewenheim

loewenheim commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

The provided test doesn't work. Despite deleting the bundle, no IntegrityError is raised and the index rows are created.

@getsantry

getsantry Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@getsantry getsantry Bot added the Stale label Oct 21, 2026
@getsantry getsantry Bot closed this Oct 28, 2026
auto-merge was automatically disabled October 28, 2026 07:00

Pull request was closed

@sentry-junior sentry-junior Bot reopened this Nov 9, 2026
@github-actions github-actions Bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Nov 9, 2026
The foreign key from ArtifactBundleIndex to ArtifactBundle is deferred, so
Postgres checks it on COMMIT, not on INSERT. Catching IntegrityError around
bulk_create never fired. Catch it around the whole transaction instead, and
re-raise unless the bundle is actually gone.

The regression test uses TransactionTestCase, because TestCase never commits
and so never runs the deferred check.

Fixes SENTRY-5HG7

Co-Authored-By: Rudra Patel <rudra.patel@sentry.io>
@sentry-junior
sentry-junior Bot force-pushed the seer/fix/artifact-bundle-indexing-race branch from 128bdcf to 1fc8d25 Compare November 9, 2026 21:16
@RudraPatel2003 RudraPatel2003 added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Nov 9, 2026
@getsantry getsantry Bot removed the Stale label Nov 10, 2026
Co-Authored-By: Rudra Patel <rudra.patel@sentry.io>
@github-actions github-actions Bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Nov 20, 2026
@RudraPatel2003 RudraPatel2003 added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Nov 20, 2026
@github-actions github-actions Bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Nov 20, 2026
@RudraPatel2003 RudraPatel2003 added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Nov 20, 2026

This branch was successfully deployed

1 active deployment
Preview — 0c36f16c Deployed Nov 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants