Repository: pulp/pulpcore

Issue:
Export artifact-dedup optimizations break pulp_rpm export.
**Version**
main, 18, 21, 22, 23, 28, 29

**Describe the bug**
After the changes for #4159 were released, pulp_rpm nightly tests started failing. This turns out to be the result of a subtle bug in the optimizations introduced for that fix.

This has to be fixed and released ASAP to all versions #4159 was releasxed into.

**To Reproduce**
* Run https://github.com/pulp/pulp_rpm/blob/main/pulp_rpm/tests/functional/api/test_pulpimport.py#L287





Discussion:

Resolving patch:
diff --git a/pulpcore/app/importexport.py b/pulpcore/app/importexport.py
--- a/pulpcore/app/importexport.py
+++ b/pulpcore/app/importexport.py
@@ -94,14 +94,14 @@ def export_artifacts(export, artifacts):
 
     Args:
         export (django.db.models.PulpExport): export instance that's doing the export
-        artifacts (django.db.models.Artifacts): QuerySet of artifacts in all repos being exported
+        artifacts (django.db.models.Artifacts): Set of artifacts in all repos being exported
 
     Raises:
         ValidationError: When path is not in the ALLOWED_EXPORT_PATHS setting
     """
     data = dict(message="Exporting Artifacts", code="export.artifacts", total=len(artifacts))
     with ProgressReport(**data) as pb:
-        for artifact in artifacts.iterator():  # chunk_size= defaults to 2000 at a fetch
+        for artifact in pb.iter(artifacts):
             dest = artifact.file.name
             if settings.DEFAULT_FILE_STORAGE != "pulpcore.app.models.storage.FileSystem":
                 with tempfile.TemporaryDirectory(dir=".") as temp_dir:
@@ -112,7 +112,6 @@ def export_artifacts(export, artifacts):
                         export.tarfile.add(temp_file.name, dest)
             else:
                 export.tarfile.add(artifact.file.path, dest)
-            pb.increment()
 
     resource = ArtifactResource()
     resource.queryset = artifacts
diff --git a/pulpcore/app/tasks/export.py b/pulpcore/app/tasks/export.py
--- a/pulpcore/app/tasks/export.py
+++ b/pulpcore/app/tasks/export.py
@@ -495,7 +495,7 @@ def _do_export(pulp_exporter, tar, the_export):
     starting_versions = _get_starting_versions(do_incremental, pulp_exporter, the_export)
     vers_match = _version_match(ending_versions, starting_versions)
     # Gather up versions and artifacts
-    artifacts = None  # Will be a QuerySet selecting the Artifacts that need to be exported
+    artifacts = set()
     for version in ending_versions:
         # Check version-content to make sure we're not being asked to export
         # an on_demand repo
@@ -504,14 +504,11 @@ def _do_export(pulp_exporter, tar, the_export):
             raise RuntimeError(_("Remote artifacts cannot be exported."))
 
         if do_incremental:
-            vers_artifacts = version.artifacts.difference(vers_match[version].artifacts)
+            vers_artifacts = version.artifacts.difference(vers_match[version].artifacts).all()
         else:
-            vers_artifacts = version.artifacts
+            vers_artifacts = version.artifacts.all()
 
-        if artifacts:
-            artifacts.union(vers_artifacts)
-        else:
-            artifacts = vers_artifacts
+        artifacts.update(vers_artifacts)
 
     # export plugin-version-info
     export_versions(the_export, plugin_version_info)


Test patch: