-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-116738: Make _codecs
module thread-safe
#117530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks pretty good. Two comments below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good. A few comments below.
…istry's return type
This should be ready to go as of my last commit - let me know if any other changes are needed. |
The module itself is a thin wrapper around calls to functions in `Python/codecs.c`, so that's where the meaningful changes happened: - Move codecs-related state that lives on `PyInterpreterState` to a struct declared in `pycore_codecs.h`. - In free-threaded builds, add a mutex to `codecs_state` to synchronize operations on `search_path`. Because `search_path_mutex` is used as a normal mutex and not a critical section, we must be extremely careful with operations called while holding it. - The codec registry is explicitly initialized as part of `_PyUnicode_InitEncodings` to simplify thread-safety.
The module itself is a thin wrapper around calls to functions in
Python/codecs.c
, so that's where the meaningful changes happened:Move codecs-related state that lives on
PyInterpreterState
to a struct declared inpycore_codecs.h
.In free-threaded builds, add two mutexes to
codecs_state
, one to synchronize operations onsearch_path
, and one to synchronize initialization._PyCodecRegistry_Init()
is now_PyCodecRegistry_EnsureInit()
, and is called unconditionally to ensure initialization, rather than only when initialization is needed. When it returns 0 (as opposed to -1 to indicate an error), thePyObject *
members ofcodecs_state
can be read without further synchronization.Operations that mutate
codecs.search_path
must be performed while holdingcodecs.search_path_mutex
. This allowsPyCodec_Unregister()
to search for and delete a specific item from the list. Becausesearch_path_mutex
is used as a normal mutex and not a critical section, we must be extremely careful with operations called while holding it.Issue: Audit all built-in modules for thread safety #116738