GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PySequence_Fast needs new macros to be safe in a nogil world · Issue #119247 · python/cpython

PySequence_Fast needs new macros to be safe in a nogil world #119247

New issue
Copy link
New issue
Copy link
Closed
Closed
PySequence_Fast needs new macros to be safe in a nogil world#119247
Copy link
Assignees
MojoVampire
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement

Description

@MojoVampire
MojoVampire
opened on May 20, 2024
Issue body actions

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. Similarly, if individual items are replaced without changing size, you'd have similar issues.

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.

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 Dino during CPython core sprints.

Linked PRs

  • gh-119247: Add macros to use PySequence_Fast safely in free-threaded build #119315
  • [3.13] gh-119247: Add macros to use PySequence_Fast safely in free-threaded build (GH-119315) #119419

Activity

  1. MojoVampire
    added
    type-featureA feature request or enhancementA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    topic-free-threading
    on May 20, 2024
  2. MojoVampire
    self-assigned this
    on May 20, 2024
  3. colesbury
    added a commit that references this issue on May 22, 2024

    gh-119247: Add macros to use PySequence_Fast safely in free-threaded …

    baf347d
  4. miss-islington
    added a commit that references this issue on May 22, 2024

    pythongh-119247: Add macros to use PySequence_Fast safely in free-thr…

    5860dc7
  5. MojoVampire commented on May 22, 2024

    @MojoVampire
    MojoVampire
    on May 22, 2024
    ContributorAuthor
    More actions

    Hey @DinoV and @colesbury: Now that the base patch is merged, should I:

    1. Close this as complete and open new issue(s) to apply similar fixes to other uses of PySequence_Fast* APIs?
    2. Reuse this issue for similar changes?
    3. Skip working on this task specifically, and instead make more core Python objects work free-threaded (e.g. I started working on bytearray to make its use of PySequence_Fast* stuff safe, but it's clearly not free-threaded safe in general yet, so maybe I just work on making bytearray as a whole free-threaded safe?).
  6. colesbury commented on May 22, 2024

    @colesbury
    colesbury
    on May 22, 2024
    Contributor
    More actions

    Hi Josh - thanks for fixing str.join. We typically reuse the same issue for related changes. As to whether to continue with PySequence_Fast* or work other changes like bytearray, that's up to you -- both sound really useful.

  7. colesbury
    added a commit that references this issue on May 22, 2024

    [3.13] gh-119247: Add macros to use PySequence_Fast safely in free-th…

    0841606
  8. eendebakpt commented on May 24, 2024

    @eendebakpt
    eendebakpt
    on May 24, 2024
    Contributor
    More actions

    @MojoVampire @colesbury In the _json module the PySequence_Fast API is used in a way that is not thread-safe. I am working on a PR, but could use some advice. The new macro Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST starts with a { (I suspect to avoid name clashes), but that makes it hard to use when the critical section used return statements or goto statements.

    In #119438 I added an extra macro Py_RETURN_CRITICAL_SECTION_SEQUENCE_FAST to address this, but I am not convinced this is a nice solution. Another option would be to refactor the code in question to eliminate the goto and return statements inside the critical part, but that will result in quite some churn of the code (and perhaps less readable code)

  9. estyxx
    added a commit that references this issue on Jul 17, 2024

    pythongh-119247: Add macros to use PySequence_Fast safely in free-thr…

    36480ac
  10. ZeroIntensity
    closed this as completedon Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @MojoVampire
    MojoVampire

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.