diff --git a/certifi/core.py b/certifi/core.py index 1c9661cc..e83593e5 100644 --- a/certifi/core.py +++ b/certifi/core.py @@ -6,6 +6,14 @@ """ import sys import atexit +import threading + +# where() lazily materialises the CA bundle and publishes it through module +# globals. The `is None` test and the assignment are separate operations, so +# without this lock several threads can each enter the resource context and +# register their own atexit hook while only the last _CACERT_CTX assignment +# survives, orphaning the others. +_CACERT_LOCK = threading.Lock() def exit_cacert_ctx() -> None: _CACERT_CTX.__exit__(None, None, None) # type: ignore[union-attr] @@ -27,19 +35,26 @@ def where() -> str: global _CACERT_CTX global _CACERT_PATH if _CACERT_PATH is None: - # This is slightly janky, the importlib.resources API wants you to - # manage the cleanup of this file, so it doesn't actually return a - # path, it returns a context manager that will give you the path - # when you enter it and will do any cleanup when you leave it. In - # the common case of not needing a temporary file, it will just - # return the file system location and the __exit__() is a no-op. - # - # We also have to hold onto the actual context manager, because - # it will do the cleanup whenever it gets garbage collected, so - # we will also store that at the global level as well. - _CACERT_CTX = as_file(files("certifi").joinpath("cacert.pem")) - _CACERT_PATH = str(_CACERT_CTX.__enter__()) - atexit.register(exit_cacert_ctx) + with _CACERT_LOCK: + if _CACERT_PATH is not None: + return _CACERT_PATH + # This is slightly janky, the importlib.resources API wants you to + # manage the cleanup of this file, so it doesn't actually return a + # path, it returns a context manager that will give you the path + # when you enter it and will do any cleanup when you leave it. In + # the common case of not needing a temporary file, it will just + # return the file system location and the __exit__() is a no-op. + # + # We also have to hold onto the actual context manager, because + # it will do the cleanup whenever it gets garbage collected, so + # we will also store that at the global level as well. + ctx = as_file(files("certifi").joinpath("cacert.pem")) + path = str(ctx.__enter__()) + _CACERT_CTX = ctx + atexit.register(exit_cacert_ctx) + # published last, so no thread can observe a path whose + # context manager is not yet reachable for cleanup + _CACERT_PATH = path return _CACERT_PATH @@ -62,20 +77,27 @@ def where() -> str: global _CACERT_CTX global _CACERT_PATH if _CACERT_PATH is None: - # This is slightly janky, the importlib.resources API wants you - # to manage the cleanup of this file, so it doesn't actually - # return a path, it returns a context manager that will give - # you the path when you enter it and will do any cleanup when - # you leave it. In the common case of not needing a temporary - # file, it will just return the file system location and the - # __exit__() is a no-op. - # - # We also have to hold onto the actual context manager, because - # it will do the cleanup whenever it gets garbage collected, so - # we will also store that at the global level as well. - _CACERT_CTX = get_path("certifi", "cacert.pem") - _CACERT_PATH = str(_CACERT_CTX.__enter__()) - atexit.register(exit_cacert_ctx) + with _CACERT_LOCK: + if _CACERT_PATH is not None: + return _CACERT_PATH + # This is slightly janky, the importlib.resources API wants you + # to manage the cleanup of this file, so it doesn't actually + # return a path, it returns a context manager that will give + # you the path when you enter it and will do any cleanup when + # you leave it. In the common case of not needing a temporary + # file, it will just return the file system location and the + # __exit__() is a no-op. + # + # We also have to hold onto the actual context manager, because + # it will do the cleanup whenever it gets garbage collected, so + # we will also store that at the global level as well. + ctx = get_path("certifi", "cacert.pem") + path = str(ctx.__enter__()) + _CACERT_CTX = ctx + atexit.register(exit_cacert_ctx) + # published last, so no thread can observe a path whose + # context manager is not yet reachable for cleanup + _CACERT_PATH = path return _CACERT_PATH