Skip to content

PyDict_Next should not lock the dict #120858

Closed

Description

@colesbury

Bug report

PyDict_Next currently wraps _PyDict_Next in a critical section. We shouldn't do this -- the locking needs to be external to the call.

  1. It's not sufficient to lock the dict just for each _PyDict_Next call because we return borrowed references and because pos becomes meaningless if the dictionary gets resized or rehashed.
  2. It interferes with externally locking the dict because the inner critical sections can suspend the outer ones. In other words, if the caller use a critical section to lock the dict for multiple iterations, this will break that.

cpython/Objects/dictobject.c

Lines 2883 to 2890 in 8f17d69

PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
{
int res;
Py_BEGIN_CRITICAL_SECTION(op);
res = _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Py_END_CRITICAL_SECTION();
return res;
}

cc @DinoV

Linked PRs

Activity

  1. changed the title [-]`PyDict_Next` should include begin a critical section[/-] [+]`PyDict_Next` should not begin a critical section[/+] on Jun 21, 2024
  2. changed the title [-]`PyDict_Next` should not begin a critical section[/-] [+]`PyDict_Next` should not lock the dict[/+] on Jun 21, 2024
  3. added 2 commits that reference this issue on Jun 21, 2024
  4. added a commit that references this issue on Jun 24, 2024
  5. added a commit that references this issue on Jun 24, 2024
  6. added a commit that references this issue on Jun 30, 2024
  7. added a commit that references this issue on Jul 11, 2024
  8. added a commit that references this issue on Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixestopic-free-threadingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions