cpython icon indicating copy to clipboard operation
cpython copied to clipboard

PySequence_Fast needs new macros to be safe in a nogil world

Open MojoVampire opened this issue 1 year ago • 0 comments

Feature or enhancement

Proposal:

Right now, most uses of PySequence_Fast are invalid in a nogil context when it is passed an existing list; PySequence_FAST_ITEMS returns a reference to the internal array of PyObject*s that can be resized at any time if other threads add or delete items, PySequence_FAST_GET_SIZE similarly reports a size that is invalid an instant after it's reported.

But when the argument passed is a tuple (incref-ed and returned unchanged, but safe due to immutability) or any non-list type (converted to new list) no lock is needed. Per conversation with Dino, going to create macros, to be called after a call to PySequence_Fast, to conditionally lock and unlock the original list when applicable, while avoiding locks in all other cases, before any other PySequence* APIs are used.

Preliminary (subject to bike-shedding) macro names are:

Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST Py_END_CRITICAL_SECTION_SEQUENCE_FAST

both defined in pycore_critical_section.h (again, subject to bike-shedding; maybe belongs in abstract.h with the rest of the PySequence APIs?).

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

Discussion occurred with @DinoV during CPython core sprints.

MojoVampire avatar May 20 '24 19:05 MojoVampire