Skip to content

gh-163237: Make mimetypes initialization and add_type() thread-safe - #163238

Open
eg-bole wants to merge 1 commit into
python:mainfrom
eg-bole:fix-mimetypes-free-threading
Open

eg-bole wants to merge 1 commit into
python:mainfrom
eg-bole:fix-mimetypes-free-threading

Conversation

@eg-bole

@eg-bole eg-bole commented Dec 6, 2026

Copy link
Copy Markdown

Summary

  • Guard lazy _db initialization via double-checked locking (_get_db()) with a module-level _thread.RLock so read-only lookups (guess_file_type(), guess_type(), guess_extension(), guess_all_extensions()) incur zero lock overhead once initialized, while concurrent first-time callers do not overwrite _db or discard add_type() registrations.
  • Ensure inited = True is only published once init() completes while avoiding recursive init() calls from MimeTypes.__init__().
  • Synchronize module-level mimetypes.add_type() updates on the shared _db and snapshot non-strict inverse mappings in guess_all_extensions() to prevent duplicate extensions under concurrent writes, while keeping independent MimeTypes() instances completely lock-free.
  • Add free-threading regression tests in Lib/test/test_free_threading/test_mimetypes.py.

@eg-bole
eg-bole requested a review from a team as a code owner December 6, 2026 13:41
@python-cla-bot

python-cla-bot Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant