fix: scope downloaded_files per crawl to prevent race condition (#1889)#1891
Open
hafezparast wants to merge 1 commit intounclecode:developfrom
Open
fix: scope downloaded_files per crawl to prevent race condition (#1889)#1891hafezparast wants to merge 1 commit intounclecode:developfrom
hafezparast wants to merge 1 commit intounclecode:developfrom
Conversation
…tamination (unclecode#1889) _downloaded_files was shared mutable state on self with no synchronization. When a slow download from URL A completed after URL B started, the file appeared on URL B's CrawlResult instead of URL A's. Replace the shared list with a dict keyed by crawl invocation ID: - Each _crawl_web call gets a unique crawl_id - Downloads are scoped to their originating crawl_id via closure - Late downloads (after the crawl popped its list) are logged but don't contaminate other crawl results Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
discopops
approved these changes
Apr 1, 2026
discopops
left a comment
There was a problem hiding this comment.
Approved. The regression harness change is minimal, the lockfile churn was cleaned, and the race-condition fix validates with pytest -q tests/test_download_race_condition_1889.py.
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.
Summary
downloaded_filesfrom URL A leak into URL B'sCrawlResultwhen downloads are slowself._downloaded_fileswas shared mutable state with fire-and-forgetasyncio.create_taskdownload handlers — no synchronization between concurrent crawlsdict[crawl_id, list]keyed by a unique ID per_crawl_webinvocation, passed into_handle_downloadvia closureFixes #1889
The Race (before)
After Fix
Changes
crawl4ai/async_crawler_strategy.py: Replaceself._downloaded_files(list) withself._downloads_by_crawl(dict keyed by crawl_id)._handle_downloadnow takes acrawl_idparameter. Late downloads that complete after their crawl finished are logged with a warning instead of silently contaminating the next crawl.tests/test_download_race_condition_1889.py: 9 tests covering isolation, late downloads, concurrent race simulationTest plan
pytest tests/test_download_race_condition_1889.py -v)🤖 Generated with Claude Code