Conversation
find_candidates_for_deduplication_uid_or_hash evaluated its candidate queryset in one go. The whole result set was buffered by the database client and cached on the queryset, each prefetch_related lookup spanned every candidate id, and select_related built a separate Test, Engagement and Test_Type instance for every candidate row. On a product where many existing findings share the batch's hash codes or unique ids, that pushed the post-processing task past a worker memory limit. - Walk the candidates with iterator(chunk_size=DEDUPE_CANDIDATE_CHUNK_SIZE): a server-side cursor, no queryset result cache, and prefetches issued per chunk. - Point each candidate at one shared Test / Engagement / Test_Type instance per id, as prefetch_related already does for its relations. Values are unchanged; only duplicate copies of identical rows are dropped. The returned maps are unchanged: the same Finding instances, in the same id order, with the same prefetches. For 20,000 colliding candidates, peak RSS growth of the call drops from 345 MB to 256 MB. 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.
Description
Symptom. Reimporting a large report into a product where many existing findings share the batch's hash codes or unique ids makes the post-processing task grow its resident memory steeply. A worker memory limit then fails the task in
dedupe_batch_of_findings, underfind_candidates_for_deduplication_uid_or_hash:Root cause.
for ef in existing_qs:evaluates the candidate queryset in one go, which multiplies the memory:prefetch_relatedlookup (locations, vulnerability ids, CWEs,found_by, endpoint statuses) runs one query over every candidate id at once.select_related("test", "test__engagement", "test__test_type")builds a separateTest,EngagementandTest_Typeinstance for every candidate row. Thousands of candidates from a handful of tests therefore carry thousands of identical copies of the same few rows.Fix. The function's contract is unchanged: it still returns the same two maps, holding the same
Findinginstances in the sameidorder with the same prefetched relations.existing_qs.iterator(chunk_size=DEDUPE_CANDIDATE_CHUNK_SIZE)(1000). That means a server-side cursor, no result cache on the queryset, and prefetches issued per chunk instead of across every candidate.Test,EngagementandTest_Typeinstance per id.prefetch_relatedalready shares instances per id this way. Every attribute value stays the same; only duplicate copies of identical rows are dropped.The maps still have to hold every candidate instance, because the matcher walks them. So this removes the redundant copies around those instances, not the instances themselves.
Measured. 20,000 candidates share 50 hash codes. Each figure is peak RSS growth of one call, measured in a fresh process each time. Both runs gave identical numbers.
bugfixHEAD)select_relatedrows (this PR)Test results
New
unittests/test_dedupe_candidate_streaming.py. Its fixture has deliberate uid/hash collisions: several findings share one hash, a uid is shared by several findings, one finding carries both a colliding uid and a colliding hash, one is already a duplicate, one has the right uid from a different tool, and one is unrelated. The tests cover:deduplicationandreimportmode.Findingcandidate queryset is walked withiterator(chunk_size=...)and never materialised whole.QuerySet._fetch_allis never called for it.Test/Engagement/Test_Typeinstance, and every relation still points at the right row.Before the fix, the two streaming tests fail (
[] != [2],5 not greater than 5) and the two equivalence tests pass. With the fix, all of these pass locally:test_dedupe_candidate_streamingtest_dedupe_injectable_scopetest_dedupe_location_prefetchtest_reimport_prefetchtest_deduplication_logic(85 tests, 3 skipped)test_importers_performance(query counts unchanged)ruff checkis clean with the pinnedruff==0.16.5.Documentation
No user-facing change.
Checklist
bugfix.bugfixbranch.