Repository navigation
Conversation
The 'if _CACERT_PATH is None' test and the assignment that publishes the path are separate operations. Concurrent callers could each enter their own resource context and register their own atexit hook, but only the last _CACERT_CTX assignment survived, orphaning the earlier context managers. Under zipimport as_file() extracts a temporary file, so an orphan is a file nothing will clean up, and the duplicate atexit hooks can close a context whose path another thread is still holding. Use double-checked locking and publish _CACERT_PATH last, so no thread can observe a path whose context manager is not yet reachable for cleanup. Both the importlib.resources branch and the fallback branch are covered. The new test fails 5/5 runs without this change and passes with it.
| ) | ||
|
|
||
|
|
||
| class TestWhereThreadSafety(unittest.TestCase): |
There was a problem hiding this comment.
this test involves so much monkeypatching that I'm personally pretty skeptical of its usefulness.
There was a problem hiding this comment.
I think we can probably drop teh test entirely tbh
There was a problem hiding this comment.
Thanks, agreed. I've dropped the test in d01ce3c, so the PR now only touches certifi/core.py, and mypy --strict passes again.
The race is easiest to see in the diff itself: the _CACERT_PATH is None check and the assignment that publishes the path were separate steps. Concurrent first calls could each open their own resource context and register their own atexit hook, and only the last one survived. Under zipimport, that leaves an extracted temp file that nothing cleans up. The fix uses double-checked locking and publishes the path last.
Happy to adjust anything else.
As suggested in review, the test relied on too much monkeypatching to be a useful regression check, and it did not pass mypy --strict. The core.py change is unchanged.
The problem
where()materialises the CA bundle lazily:The
is Nonetest and the assignment that publishes the path are separate operations. Concurrent callers can all pass the test; each opens its own context manager and registers its ownatexithook, but only the last assignment to_CACERT_CTXsurvives, so the earlier context managers are orphaned — nothing holds them, andexit_cacert_ctxruns once per extra registration against whichever context happens to be current.In the common filesystem install
as_filereturns the real path and__exit__is a no-op, so the damage is bounded to redundantatexitregistrations. When certifi is imported from a zip or wheel,as_fileextracts a temporary file and__exit__deletes it — then an orphaned context is a temp file nobody cleans up, and the duplicate hooks can delete a path another thread is still holding.This is not free-threading-specific
It reproduces with the GIL enabled too — the GIL just hid it after the first call warmed the cache. Measured with 16 threads over 200 trials on CPython 3.14.4:
PYTHON_GIL=1So this is a general thread-safety bug that free-threading makes reliable rather than occasional.
The change
Double-checked locking, with
_CACERT_PATHpublished last so no thread can observe a path whose context manager is not yet reachable for cleanup. Both thesys.version_info >= (3, 11)branch and the fallback branch are patched.Tests
TestWhereThreadSafetyincertifi/tests/test_certify.pycounts entries into the resource context by temporarily replacingcertifi.core.atexit.registerwith a counter, and asserts exactly one per cold start. It fails 5/5 runs without the change and passes with it.It waits on an
Eventrather than aBarrierand skips if fewer than two threads can be started, so a thread-constrained runner degrades rather than deadlocks. All globals and the patchedregisterare restored in afinally.Full suite: 4 passed, both free-threaded and with
PYTHON_GIL=1.Environment: CPython 3.14.4 free-threaded, macOS 15 / arm64.