FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-159739: Hold the codec registry lock while unregistering by jelle-openai · Pull Request #160168 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in free-threaded builds when :func:`codecs.unregister` is
called concurrently.
19 changes: 8 additions & 11 deletions Python/codecs.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,21 @@ PyCodec_Unregister(PyObject *search_function)

PyObject *codec_search_path = interp->codecs.search_path;
assert(PyList_CheckExact(codec_search_path));
FT_MUTEX_LOCK(&interp->codecs.search_path_mutex);
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(codec_search_path); i++) {
FT_MUTEX_LOCK(&interp->codecs.search_path_mutex);
PyObject *item = PyList_GetItemRef(codec_search_path, i);
int ret = 1;
if (item == search_function) {
// We hold a reference to the item, so its destructor can't run
// while we hold search_path_mutex.
ret = PyList_SetSlice(codec_search_path, i, i+1, NULL);
}
FT_MUTEX_UNLOCK(&interp->codecs.search_path_mutex);
Py_DECREF(item);
if (ret != 1) {
if (PyList_GET_ITEM(codec_search_path, i) == search_function) {
// Keep the function alive until after releasing the mutex.
Py_INCREF(search_function);
int ret = PyList_SetSlice(codec_search_path, i, i+1, NULL);
FT_MUTEX_UNLOCK(&interp->codecs.search_path_mutex);
Py_DECREF(search_function);
assert(interp->codecs.search_cache != NULL);
assert(PyDict_CheckExact(interp->codecs.search_cache));
PyDict_Clear(interp->codecs.search_cache);
return ret;
}
}
FT_MUTEX_UNLOCK(&interp->codecs.search_path_mutex);
return 0;
}

Expand Down
Loading

Back | FazBrowse Home | New Git URL