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

[3.13] gh-149816: Fix race condition in `memoryview` with free-threading (GH-149858) by sobolevn · Pull Request #149877 · python/cpython · GitHub

/ cpython Public
Closed
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 @@
Fix a race condition in :class:`memoryview` with free-threading.
21 changes: 15 additions & 6 deletions Objects/memoryobject.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 @@ -67,6 +67,13 @@ class memoryview "PyMemoryViewObject *" "&PyMemoryView_Type"
releasebufferprocs must NOT decrement view.obj.
*/

#ifdef Py_GIL_DISABLED
#define _FT_ATOMIC_ADD_SSIZE(value, new_value) \
(void)_Py_atomic_add_ssize(&value, new_value)
#else
#define _FT_ATOMIC_ADD_SSIZE(value, new_value) (void)(value += new_value)
#endif


static inline _PyManagedBufferObject *
mbuf_alloc(void)
Expand Down Expand Up @@ -1589,7 +1596,7 @@ memory_getbuf(PyObject *_self, Py_buffer *view, int flags)


view->obj = Py_NewRef(self);
self->exports++;
_FT_ATOMIC_ADD_SSIZE(self->exports, 1);

return 0;
}
Expand All @@ -1598,7 +1605,7 @@ static void
memory_releasebuf(PyObject *_self, Py_buffer *view)
{
PyMemoryViewObject *self = (PyMemoryViewObject *)_self;
self->exports--;
_FT_ATOMIC_ADD_SSIZE(self->exports, -1);
return;
/* PyBuffer_Release() decrements view->obj after this function returns. */
}
Expand Down Expand Up @@ -2337,9 +2344,9 @@ memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
// Prevent 'self' from being freed if computing len(sep) mutates 'self'
// in _Py_strhex_with_sep().
// See: https://github.com/python/cpython/issues/143195.
self->exports++;
_FT_ATOMIC_ADD_SSIZE(self->exports, 1);
PyObject *ret = _Py_strhex_with_sep(src->buf, src->len, sep, bytes_per_sep);
self->exports--;
_FT_ATOMIC_ADD_SSIZE(self->exports, -1);
return ret;
}

Expand Down Expand Up @@ -3081,9 +3088,9 @@ memory_hash(PyObject *_self)
if (view->obj != NULL) {
// Prevent 'self' from being freed when computing the item's hash.
// See https://github.com/python/cpython/issues/142664.
self->exports++;
_FT_ATOMIC_ADD_SSIZE(self->exports, 1);
Py_hash_t h = PyObject_Hash(view->obj);
self->exports--;
_FT_ATOMIC_ADD_SSIZE(self->exports, -1);
if (h == -1) {
/* Keep the original error message */
return -1;
Expand Down Expand Up @@ -3457,3 +3464,5 @@ PyTypeObject PyMemoryView_Type = {
0, /* tp_alloc */
memoryview, /* tp_new */
};

#undef _FT_ATOMIC_ADD_SSIZE
Loading

Back | FazBrowse Home | New Git URL