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 — make memoryview thread safe in free-threading · Issue #127716 · python/cpython

make memoryview thread safe in free-threading #127716

New issue
Copy link
New issue
Copy link
Closed
Closed
make memoryview thread safe in free-threading#127716
Copy link
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

@kumaraditya303
kumaraditya303
opened on Dec 7, 2024
Issue body actions

memoryview is currently not safe against free-threading especially concurrent releasing the memoryviews. This issue track work to make it thread safe.

This is a follow-up to #127412 (review).

Linked PRs

  • gh-127716: Fix data race in memoryview.release() #154770
  • gh-127716: make memoryview thread-safe in free-threaded build #156248

Activity

Next
  1. kumaraditya303
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    topic-free-threading
    3.14bugs and security fixesbugs and security fixes
    on Dec 7, 2024
  2. kumaraditya303
    self-assigned this
    on Dec 7, 2024
  3. picnixz commented on Dec 7, 2024

    @picnixz
    picnixz
    on Dec 7, 2024
    Member
    More actions

    Do you think it should be a feature (main only) or a bug fix (3.13+)?

  4. kumaraditya303 commented on Dec 7, 2024

    @kumaraditya303
    kumaraditya303
    on Dec 7, 2024
    ContributorAuthor
    More actions

    For now I'll target main, if it doesn't need too many changes it may be backported.

  5. picnixz
    added
    type-featureA feature request or enhancementA feature request or enhancement
    and removed
    3.14bugs and security fixesbugs and security fixes
    on Dec 7, 2024
  6. picnixz commented on Dec 7, 2024

    @picnixz
    picnixz
    on Dec 7, 2024
    Member
    More actions

    Ok. I'll apply the labels after we decided whether we should backport it or not.

  7. ZeroIntensity commented on Dec 7, 2024

    @ZeroIntensity
    ZeroIntensity
    on Dec 7, 2024
    Member
    More actions

    FWIW, we typically do backport thread safety fixes to 3.13. We should probably merge gh-127412 before putting up anything new to prevent conflicts.

  8. serhiy-storchaka
    changed the title [-]make memoryview thread safe in free-theading[/-] [+]make memoryview thread safe in free-threading[/+] on Dec 13, 2024
  9. kumaraditya303
    removed their assignment
    on Mar 17, 2025
  10. serhiy-storchaka
    added this to Struct, memoryview and array issues 🏗️on Jul 13, 2026
  11. github-project-automation
    moved this to Todo in Struct, memoryview and array issues 🏗️on Jul 13, 2026
  12. devdanzin commented on Jul 25, 2026

    @devdanzin
    devdanzin
    on Jul 25, 2026
    Member
    More actions

    A concrete, crashing reproducer for the "concurrent releasing" part of this issue. On a free-threaded build, several threads calling memoryview.release() on views of one shared buffer (interleaved with reads of a view) underflow the non-atomic export counters and abort.

    import threading
    buf = bytearray(64)
    cell = [memoryview(buf)]
    NT = 8
    ITERS = 40000
    
    def worker(w):
        for _ in range(ITERS):
            mv = cell[0]
            try:
                if w % 2 == 0:
                    mv.release()
                    cell[0] = memoryview(buf)
                else:
                    _ = mv.readonly
                    _ = mv[0]
            except Exception:
                pass
    
    ts = [threading.Thread(target=worker, args=(w,)) for w in range(NT)]
    for t in ts: t.start()
    for t in ts: t.join()

    On a free-threaded debug build (PYTHON_GIL=0) this aborts on essentially every run. The exact assertion that fires depends on the build/scheduling — both are the same root cause, a non-atomic export counter driven below its invariant by concurrent release():

    • Debug build with ASan — the memoryview's own mbuf->exports:

      Objects/memoryobject.c:1116: void _memory_release(PyMemoryViewObject *):
          Assertion `self->mbuf->exports > 0' failed.
      

      _memory_release (memoryobject.c:1115) decrements self->mbuf->exports non-atomically, so
      concurrent releases drive it to/below zero.

    • Plain debug build (no ASan) — the exporter bytearray's ob_exports:

      Objects/bytearrayobject.c:89: void bytearray_releasebuffer(PyObject *, Py_buffer *):
          Assertion `obj->ob_exports >= 0' failed.
      

      the release path double-decrements the exporter's export count (also non-atomic), so it goes negative.

    Both reproduce 5/5 here. Under TSan the same race shows up as data-race stanzas pairing the release side against the read side, e.g. _memory_release / mbuf_release vs memory_readonly_get / memory_item / memory_subscript — and the read of a concurrently-released view is also a use-after-release angle.

    This is the release side of the exports counter, which looks still-open under this issue — distinct from the memoryview.hex exports race that was fixed (gh-149858 / item 132 of #149816) and the earlier ->exports fix in gh-127085 (#127412) that this issue is the follow-up to.

    Found with fusil's --tsan mode; reduced and drafted with AI assistance (Claude Code), verified by hand on free-threaded debug / ASan / TSan builds of main (3.16.0a0).

  13. 1 remaining item

  14. weixlu commented on Jul 27, 2026

    @weixlu
    weixlu
    on Jul 27, 2026
    Contributor
    More actions

    @devdanzin Hi, I submitted a PR #154770 trying to fix the issue you mentioned. I think self->flags need to be protected by atomic operation, then only one thread would enter the release path

  15. nascheme
    added 4 commits that reference this issue on Aug 23, 2026

    pythongh-127716: make memoryview thread-safe

    4269e2d

    pythongh-127716: make memoryview thread-safe

    8fa2daf

    Merge remote-tracking branch 'origin/main' into pythongh-127716-memor…

    dd6c824

    Merge remote-tracking branch 'origin/main' into pythongh-127716-memor…

    ab2d602
  16. nascheme
    added a commit that references this issue on Oct 3, 2026

    Merge remote-tracking branch 'origin/main' into pythongh-127716-memor…

    25f396f
  17. nascheme
    added 2 commits that reference this issue on Oct 21, 2026

    Merge remote-tracking branch 'origin/main' into pythongh-127716-memor…

    b9b2ad1

    Merge remote-tracking branch 'origin/main' into pythongh-127716-memor…

    21b16d4
  18. kumaraditya303
    added 2 commits that reference this issue on Oct 31, 2026

    Merge branch 'main' into pythongh-127716-memoryview-thread-safe

    de2ddbd

    gh-127716: make memoryview thread-safe in free-threaded build (#156248)

    7c5231e
  19. kumaraditya303
    closed this as completedon Nov 5, 2026
  20. github-project-automation
    moved this from Todo to Done in Struct, memoryview and array issues 🏗️on Nov 5, 2026
  21. snovader
    added 3 commits that reference this issue on Nov 11, 2026

    Test native string reads from threads on free-threaded builds too

    a267d15

    Add a native implementation of the Python runtime, and publish its wh…

    ea4d74e

    Generate Python that reads and writes values as tuples, and vectors a…

    e24505a
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

    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

    • Struct, memoryview and array issues 🏗️
      Status
      Done
      Show more project fields

    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.