From 1fc8d2521294bf5704906157ee85e1c5b76163a3 Mon Sep 17 00:00:00 2001 From: "sentry[bot]" <39604003+sentry[bot]@users.noreply.github.com> Date: Tue, 29 Sep 2026 10:58:29 +0000 Subject: [PATCH 1/2] fix(debug-files): Handle bundle deleted before indexing commits 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 --- src/sentry/debug_files/artifact_bundles.py | 75 +++++++++++-------- .../debug_files/test_artifact_bundles.py | 31 +++++++- 2 files changed, 71 insertions(+), 35 deletions(-) diff --git a/src/sentry/debug_files/artifact_bundles.py b/src/sentry/debug_files/artifact_bundles.py index 173aa90e8aaf..286458eb8980 100644 --- a/src/sentry/debug_files/artifact_bundles.py +++ b/src/sentry/debug_files/artifact_bundles.py @@ -7,7 +7,7 @@ import orjson import sentry_sdk from django.conf import settings -from django.db import router +from django.db import IntegrityError, router from django.db.models import Count, Exists, OuterRef from django.utils import timezone from sentry_redis_tools.clients import RedisCluster @@ -184,38 +184,47 @@ def index_urls_in_bundle( # We want to start a transaction for each bundle, so that in case of failures we keep consistency at the # bundle level, and we also have to retry only the failed bundle in the future and not all the bundles. - with atomic_transaction( - using=( - router.db_for_write(ArtifactBundle), - router.db_for_write(ArtifactBundleIndex), - ) - ): - # Since we use read committed isolation, the value we read here can change after query execution, - # but we have this check in place for analytics purposes. - bundle_was_indexed = ArtifactBundle.objects.filter( - id=artifact_bundle.id, - indexing_state=ArtifactBundleIndexingState.WAS_INDEXED.value, - ).exists() - # If the bundle was already indexed, we will skip insertion into the database. - if bundle_was_indexed: - metrics.incr("artifact_bundle_indexing.bundle_was_already_indexed") - return - - # Insert the index - # NOTE: The django ORM by default tries to batch *all* the inserts into a single query, - # which is not quite that efficient. We want to have a fixed batch size, - # which will result in a fixed number of unique `INSERT` queries. - ArtifactBundleIndex.objects.bulk_create(urls_to_index, batch_size=50) - - # Mark the bundle as indexed - ArtifactBundle.objects.filter(id=artifact_bundle.id).update( - indexing_state=ArtifactBundleIndexingState.WAS_INDEXED.value - ) - - metrics.incr("artifact_bundle_indexing.bundles_indexed") - metrics.incr("artifact_bundle_indexing.urls_indexed", len(urls_to_index)) - if skipped_urls: - metrics.incr("artifact_bundle_indexing.urls_skipped", skipped_urls) + try: + with atomic_transaction( + using=( + router.db_for_write(ArtifactBundle), + router.db_for_write(ArtifactBundleIndex), + ) + ): + # Since we use read committed isolation, the value we read here can change after query execution, + # but we have this check in place for analytics purposes. + bundle_was_indexed = ArtifactBundle.objects.filter( + id=artifact_bundle.id, + indexing_state=ArtifactBundleIndexingState.WAS_INDEXED.value, + ).exists() + # If the bundle was already indexed, we will skip insertion into the database. + if bundle_was_indexed: + metrics.incr("artifact_bundle_indexing.bundle_was_already_indexed") + return + + # Insert the index + # NOTE: The django ORM by default tries to batch *all* the inserts into a single query, + # which is not quite that efficient. We want to have a fixed batch size, + # which will result in a fixed number of unique `INSERT` queries. + ArtifactBundleIndex.objects.bulk_create(urls_to_index, batch_size=50) + + # Mark the bundle as indexed + ArtifactBundle.objects.filter(id=artifact_bundle.id).update( + indexing_state=ArtifactBundleIndexingState.WAS_INDEXED.value + ) + except IntegrityError: + # The foreign key from `ArtifactBundleIndex` to `ArtifactBundle` is deferred, so it is only + # checked on commit. An upload of the same bundle can delete this one as a duplicate in the + # meantime, which leaves nothing to index. + if ArtifactBundle.objects.filter(id=artifact_bundle.id).exists(): + raise + metrics.incr("artifact_bundle_indexing.bundle_deleted_during_indexing") + return + + metrics.incr("artifact_bundle_indexing.bundles_indexed") + metrics.incr("artifact_bundle_indexing.urls_indexed", len(urls_to_index)) + if skipped_urls: + metrics.incr("artifact_bundle_indexing.urls_skipped", skipped_urls) # ===== Renewal of Artifact Bundles ===== diff --git a/tests/sentry/debug_files/test_artifact_bundles.py b/tests/sentry/debug_files/test_artifact_bundles.py index ae3e5c84ff46..91e582a0386f 100644 --- a/tests/sentry/debug_files/test_artifact_bundles.py +++ b/tests/sentry/debug_files/test_artifact_bundles.py @@ -4,7 +4,7 @@ from hashlib import sha1 from io import BytesIO from typing import Literal, TypedDict -from unittest.mock import patch +from unittest.mock import MagicMock, patch from django.core.files.base import ContentFile from django.db import connections, router @@ -34,7 +34,7 @@ from sentry.models.files.fileblob import FileBlob from sentry.models.project import Project from sentry.tasks.assemble import assemble_artifacts -from sentry.testutils.cases import TestCase +from sentry.testutils.cases import TestCase, TransactionTestCase from sentry.testutils.helpers.options import override_options from sentry.utils import json @@ -449,6 +449,33 @@ def test_skips_files_named_after_their_debug_id(self) -> None: ] +class IndexUrlsInDeletedBundleTest(TransactionTestCase): + # The foreign key from `ArtifactBundleIndex` to `ArtifactBundle` is deferred, so Postgres only + # checks it on commit. A `TestCase` wraps each test in a transaction that never commits, so it + # would silently insert index rows for a deleted bundle. + @patch("sentry.debug_files.artifact_bundles.metrics.incr") + def test_bundle_deleted_before_commit(self, mock_incr: MagicMock) -> None: + artifact_bundle = self.create_artifact_bundle(artifact_count=1) + files: dict[str, _ArtifactFile] = { + "files/_/_/app.js": { + "url": "~/static/js/app.js", + "type": "minified_source", + "content": b"app", + }, + } + with ArtifactBundleArchive(BytesIO(make_compressed_zip_file(files))) as archive: + # An upload of the same bundle deletes this one as a duplicate after this upload + # opened the archive but before its index rows are committed. + ArtifactBundle.objects.filter(id=artifact_bundle.id).delete() + + index_urls_in_bundle(self.organization.id, artifact_bundle, archive) + + mock_incr.assert_any_call("artifact_bundle_indexing.bundle_deleted_during_indexing") + assert not ArtifactBundleIndex.objects.filter( + artifact_bundle_id=artifact_bundle.id + ).exists() + + class GetArtifactBundlesContainingUrlTest(TestCase): def setUp(self) -> None: self.release_name = "1.0.0" From d2a7032004514d488662fbb1711ca12d31bc2149 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2026 17:09:31 +0000 Subject: [PATCH 2/2] ref(debug-files): Shorten comments on deleted bundle indexing Co-Authored-By: Rudra Patel --- src/sentry/debug_files/artifact_bundles.py | 5 ++--- tests/sentry/debug_files/test_artifact_bundles.py | 7 ++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/sentry/debug_files/artifact_bundles.py b/src/sentry/debug_files/artifact_bundles.py index 286458eb8980..3278143ddd23 100644 --- a/src/sentry/debug_files/artifact_bundles.py +++ b/src/sentry/debug_files/artifact_bundles.py @@ -213,9 +213,8 @@ def index_urls_in_bundle( indexing_state=ArtifactBundleIndexingState.WAS_INDEXED.value ) except IntegrityError: - # The foreign key from `ArtifactBundleIndex` to `ArtifactBundle` is deferred, so it is only - # checked on commit. An upload of the same bundle can delete this one as a duplicate in the - # meantime, which leaves nothing to index. + # An upload of the same bundle can delete this one as a duplicate. The foreign key is + # deferred, so that only raises on commit. if ArtifactBundle.objects.filter(id=artifact_bundle.id).exists(): raise metrics.incr("artifact_bundle_indexing.bundle_deleted_during_indexing") diff --git a/tests/sentry/debug_files/test_artifact_bundles.py b/tests/sentry/debug_files/test_artifact_bundles.py index 91e582a0386f..a5d4b1367559 100644 --- a/tests/sentry/debug_files/test_artifact_bundles.py +++ b/tests/sentry/debug_files/test_artifact_bundles.py @@ -450,9 +450,7 @@ def test_skips_files_named_after_their_debug_id(self) -> None: class IndexUrlsInDeletedBundleTest(TransactionTestCase): - # The foreign key from `ArtifactBundleIndex` to `ArtifactBundle` is deferred, so Postgres only - # checks it on commit. A `TestCase` wraps each test in a transaction that never commits, so it - # would silently insert index rows for a deleted bundle. + # `TestCase` never commits, so it would never check the deferred foreign key. @patch("sentry.debug_files.artifact_bundles.metrics.incr") def test_bundle_deleted_before_commit(self, mock_incr: MagicMock) -> None: artifact_bundle = self.create_artifact_bundle(artifact_count=1) @@ -464,8 +462,7 @@ def test_bundle_deleted_before_commit(self, mock_incr: MagicMock) -> None: }, } with ArtifactBundleArchive(BytesIO(make_compressed_zip_file(files))) as archive: - # An upload of the same bundle deletes this one as a duplicate after this upload - # opened the archive but before its index rows are committed. + # Simulate an upload of the same bundle deleting this one as a duplicate. ArtifactBundle.objects.filter(id=artifact_bundle.id).delete() index_urls_in_bundle(self.organization.id, artifact_bundle, archive)