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

[3.14] gh-159739: Allocate thread-local state before constructing the object (GH-160166) by miss-islington · Pull Request #160285 · python/cpython · GitHub

Repository navigation

Merged
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 @@
Avoid a crash when a :class:`threading.local` subclass finalizer accesses
attributes after construction fails.
25 changes: 15 additions & 10 deletions Modules/_threadmodule.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 @@ -1470,10 +1470,25 @@ local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
assert(module != NULL);
thread_module_state *state = get_thread_state(module);

// A subclass finalizer may access attributes if construction fails.
PyObject *localdicts = PyDict_New();
if (localdicts == NULL) {
return NULL;
}
PyObject *thread_watchdogs = PySet_New(NULL);
if (thread_watchdogs == NULL) {
Py_DECREF(localdicts);
return NULL;
}

localobject *self = (localobject *)type->tp_alloc(type, 0);
if (self == NULL) {
Py_DECREF(localdicts);
Py_DECREF(thread_watchdogs);
return NULL;
}
self->localdicts = localdicts;
self->thread_watchdogs = thread_watchdogs;

// gh-128691: Use deferred reference counting for thread-locals to avoid
// contention on the shared object.
Expand All @@ -1482,16 +1497,6 @@ local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
self->args = Py_XNewRef(args);
self->kw = Py_XNewRef(kw);

self->localdicts = PyDict_New();
if (self->localdicts == NULL) {
goto err;
}

self->thread_watchdogs = PySet_New(NULL);
if (self->thread_watchdogs == NULL) {
goto err;
}

PyObject *localsdict = NULL;
PyObject *sentinel_wr = NULL;
if (create_localsdict(self, state, &localsdict, &sentinel_wr) < 0) {
Expand Down
Loading

Back | FazBrowse Home | New Git URL