Free-threading race conditions in `mimetypes` lazy initialization and `add_type()` · Issue #163237 · python/cpython
Repository navigation
Free-threading race conditions in mimetypes lazy initialization and add_type() #163237
New issue
New issue
Backlog
Backlog
Free-threading race conditions in
mimetypes lazy initialization and add_type() #163237
Labels
stdlib
topic-free-threading
type-bug
Description
@eg-bole's profile eg-bole opened on Dec 6, 2026Bug report
Bug report
Bug description:
In Lib/mimetypes.py, module-level functions (guess_type(), guess_file_type(), guess_extension(), guess_all_extensions(), and add_type()) lazily initialize the global _db instance without synchronization:
if _db is None:
init()
return _db.add_type(type, ext, strict)
(Historical context: gh-50103 previously moved _db = db to the end of init() to prevent infinite recursion, but left lazy initialization and module-level mutations unsynchronized.)
Under multi-threaded execution (particularly reproducible on free-threaded builds --disable-gil), this leads to three thread-safety issues:
- Lost
add_type()registrations during concurrent lazy initialization:
If one thread callsmimetypes.add_type()while another thread concurrently callsmimetypes.guess_file_type()before_dbis initialized, both threads executeinit(), create separateMimeTypesinstances, and overwrite_db = db. Whichever thread finishesinit()later silently wipes out custom MIME types registered by the earlier thread. - Premature
inited = Truevisibility:
init()setsinited = Trueat entry before reading files/registry and assigning_dband module-level maps (types_map,suffix_map, etc.), allowing concurrentMimeTypes()callers to observeinited == Truebefore initialization completes. - Duplicate extensions in module-level
mimetypes.add_type():
Module-levelmimetypes.add_type()mutates the shared global_dbwithout synchronization, performing a non-atomic check-then-append (if ext not in exts: exts.append(ext)) on_db.types_map_inv, which can insert duplicate extensions returned bymimetypes.guess_all_extensions().
Minimal reproducer:
import mimetypes
import threading
for round_idx in range(20):
mimetypes.inited = False
mimetypes._default_mime_types()
mimetypes._db = None
barrier = threading.Barrier(6)
def register_custom(idx):
barrier.wait()
mimetypes.add_type(f"application/x-custom-{idx}", f".custom{idx}")
def reader():
barrier.wait()
mimetypes.guess_file_type("index.html")
threads = [
*(threading.Thread(target=register_custom, args=(i,)) for i in range(3)),
*(threading.Thread(target=reader) for _ in range(3)),
]
for t in threads:
t.start()
for t in threads:
t.join()
for i in range(3):
got, _ = mimetypes.guess_file_type(f"sample.custom{i}")
assert got == f"application/x-custom-{i}", f"round {round_idx}: lost .custom{i} ({got})"
On a free-threaded build (python3.15t / main with --disable-gil), this assertion fails reliably on the first round.
CPython versions tested on:
- CPython
mainbranch - 3.15 free-threaded build
Operating systems tested on:
- Windows / Linux
CPython versions tested on:
3.15
Operating systems tested on:
Windows, Linux
Linked PRs
Metadata
Metadata
Assignees
No one assignedLabels
stdlib
topic-free-threading
type-bug