Skip to content

Avoid shared language_detector and locale cache in search_dates - #1371

Merged
serhii73 merged 1 commit into
scrapinghub:masterfrom
Steve0x2a:fix/search-dates-singleton-race
Sep 3, 2026
Merged

serhii73 merged 1 commit into
scrapinghub:masterfrom
Steve0x2a:fix/search-dates-singleton-race

Conversation

@Steve0x2a

Copy link
Copy Markdown
Contributor

Fixes #1369.

search_dates runs through a process-wide DateSearchWithDetection singleton. Two leftover store-then-load races from #1346 can make concurrent calls return None or a date built from the wrong locale:

  1. detect_language stashed a FullTextLanguageDetector on self and then loaded it. That detector is single-use: _best_language narrows self.languages in place. A concurrent call can observe another call's already-narrowed detector.
  2. _ExactLanguageSearch kept self.language as a 1-slot cache on the same singleton. A store from another thread between get_current_language and translate_search uses the wrong locale.

Neither value needs to be instance state. Build the detector as a local, and have get_current_language return the locale (LocaleDataLoader.get_locale already caches). Public API is unchanged.

Thanks @serhii73 for the report and the diagnosis.

Fixes scrapinghub#1369.

search_dates uses a process-wide DateSearchWithDetection singleton.
detect_language stashed a single-use FullTextLanguageDetector on self,
and _ExactLanguageSearch kept a 1-slot locale cache. Concurrent calls
that disagree about the language could return None or a date from the
wrong locale.

Use a local detector and return the locale from get_current_language so
neither value is instance state. LocaleDataLoader.get_locale already
caches.

Thanks @serhii73 for the report.
@codecov

codecov Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 97.18%. Comparing base (6b5f17d) to head (b9a0382).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1371      +/-   ##
==========================================
- Coverage   97.18%   97.18%   -0.01%     
==========================================
  Files         236      236              
  Lines        3059     3057       -2     
==========================================
- Hits         2973     2971       -2     
  Misses         86       86              

β˜” View full report in Codecov by Harness.
πŸ“’ Have feedback on the report? Share it here.

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Sep 2, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

βœ… 7 untouched benchmarks


Comparing Steve0x2a:fix/search-dates-singleton-race (b9a0382) with master (6b5f17d)

Open in CodSpeed

@serhii73
serhii73 merged commit 27182cd into scrapinghub:master Sep 3, 2026
17 checks passed
@serhii73

serhii73 commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

Thanks!

@serhii73 serhii73 mentioned this pull request Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent search_dates calls race on the _search_with_detection singleton and return wrong dates

2 participants