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 — `ctypes` thread safety auditing (and fixing) · Issue #127945 · python/cpython

ctypes thread safety auditing (and fixing) #127945

New issue
Copy link
New issue
Copy link
Closed
#134332
Closed
ctypes thread safety auditing (and fixing)#127945
#134332
Copy link
Assignees
kumaraditya303
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-ctypestopic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement

Description

@ZeroIntensity
ZeroIntensity
opened on Dec 14, 2024
Issue body actions

Feature or enhancement

This is a tracking issue for all thread safety problems related to ctypes. I'll be working on this, but others can feel free to give me some help.

First of all, we need to find where the thread safety problems are.

### Auditing
- [x] Audit `_ctypes.c`
- [ ] Audit `_ctypes_test.c` (and probably the generated file too)
- [ ] Audit `callbacks.c`
- [ ] Audit `callproc.c`
- [ ] Audit `cfield.c`
- [x] Audit `malloc_closure.c`
- [ ] Audit `stgdict.c`

I'll be tracking the issues that get found when auditing here. The plan is to just create a new issue and link to it for each new problem instead of flooding this issue with PRs.

Generally, the workflow for fixes should follow most of the rules from #116738, but I suspect we'll need recursive mutexes for quite a few things related to callbacks, because it's difficult to tell what might be re-entrant, and we can't use critical sections for arbitrary function pointers.

### Known Issues
- [ ] https://github.com/python/cpython/issues/127946
- [ ] https://github.com/python/cpython/issues/128182
- [ ] https://github.com/python/cpython/issues/128485
- [ ] https://github.com/python/cpython/issues/128567
- [ ] https://github.com/python/cpython/issues/128570
- [ ] https://github.com/python/cpython/issues/131974

cc @encukou, as the ctypes genius, and @colesbury as the free-threading mastermind.

Linked PRs

  • gh-127945: add locking to malloc closure in free-threading #131662
  • gh-127945: fix thread safety of ctypes state #131710
  • gh-127945: fix thread safety of creating instances of ctypes structures #131716
  • gh-127945: make initialization of error_object_name thread safe #131896
  • gh-127945: acquire critical section around PyCFuncPtr_call #131898
  • gh-127945: mark TestLocalization as thread unsafe in ctypes #131899
  • gh-127945: fix thread safety and add lock held assertions to paramfunc in ctypes #132473
  • gh-127945: move initialization of field desc to module exec in ctypes  #132552
  • gh-127945: change _ctypes_test static globals to thread local #132575
  • gh-127945: fix critical sections around ctypes array #132646
  • gh-127945: skip more tests in ctypes when using parallel threads  #132682
  • gh-127945: add lock held assertions in ctypes arrays  #132720
  • gh-127945: add test_ctypes to free-threading TSAN CI #132727
  • gh-127945: Update What's New in Python 3.14 for free-threaded ctypes #134332
  • [3.14] gh-127945: Update What's New in Python 3.14 for free-threaded ctypes (GH-134332) #134364
Reacted by Enzo Bonnal, vfdev and Erlend E. Aasland

Activity

Next
  1. ZeroIntensity
    added
    type-featureA feature request or enhancementA feature request or enhancement
    topic-ctypes
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    topic-free-threading
    on Dec 14, 2024
  2. encukou commented on Jan 10, 2025

    @encukou
    encukou
    on Jan 10, 2025
    Member
    More actions

    Thank you! I can review the ctypes parts :)

    Heads-up: I'm currently working in cfield.c/PyCField*; focus elsewhere to avoid conflicts.

  3. ZeroIntensity commented on Jan 10, 2025

    @ZeroIntensity
    ZeroIntensity
    on Jan 10, 2025
    MemberAuthor
    More actions

    Got it, thanks! I'll probably just move to callbacks.c next.

  4. kumaraditya303
    self-assigned this
    on Mar 20, 2025
  5. kumaraditya303 commented on Mar 20, 2025

    @kumaraditya303
    kumaraditya303
    on Mar 20, 2025
    Contributor
    More actions

    I discussed this with Peter and moving forward I'll be taking over the thread safety work and continue it.

    Reacted by Petr Viktorin
  6. kumaraditya303 commented on Mar 24, 2025

    @kumaraditya303
    kumaraditya303
    on Mar 24, 2025
    Contributor
    More actions

    I investigated the thread safety of ctypes and here's the (incomplete) list of issues:

    • Race in _ctypes_alloc_callback and libffi under free-threading #128485: race in initialization of dlmalloc in _ctypes_alloc_callback. This is fixed by allocating a small memory block and freeing it in module initialization to force initialization of the allocator and is now thread safe with gh-128485: ensure that dlmalloc initializes itself at import time in ctypes  #131633.
    • the freelist in malloc_closure.c is not thread safe in free-threading. This can be fixed by adding lock around the alloc and free functions in free-threading in gh-127945: add locking to malloc closure in free-threading #131662.
    • concurrent calls to PyCStructUnionType_update_stginfo needs to be made thread safe as it modifies the type's data. This can be fixed by adding critical section around reading and writing to stginfo data in free-threading as using just mutex can lead to re-entrancy deadlocks.
    • the initialization and concurrent modifications of stginfo is not thread safe
    • the module level functions such as resize are not thread safe and need to acquire critical section of the object being resized.
  7. colesbury commented on Mar 24, 2025

    @colesbury
    colesbury
    on Mar 24, 2025
    Contributor
    More actions

    I think there are also races on StgInfo.flags, in particular with the DICTFLAG_FINAL bit.

    cpython/Modules/_ctypes/_ctypes.c

    Lines 718 to 719 in 4596666

    info->flags &= ~DICTFLAG_FINAL; /* clear the 'final' flag in the subclass info */
    baseinfo->flags |= DICTFLAG_FINAL; /* set the 'final' flag in the baseclass info */

  8. kumaraditya303 commented on Mar 24, 2025

    @kumaraditya303
    kumaraditya303
    on Mar 24, 2025
    Contributor
    More actions

    I think there are also races on StgInfo.flags, in particular with the DICTFLAG_FINAL bit.

    Yes, I am covering that under "the initialization and concurrent modifications of stginfo is not thread safe".

  9. colesbury commented on Mar 24, 2025

    @colesbury
    colesbury
    on Mar 24, 2025
    Contributor
    More actions

    This is different and more important than concurrent modification or initialization of stginfo, and I think it should be one of the early things we should focus on. Concurrent modifications of ctypes Structures is not an important use case.

    However, creating separate instances of the same structure is an important use case:

    from ctypes import Structure, c_int
    from concurrent.futures import ThreadPoolExecutor
    
    class POINT(Structure):
        _fields_ = [("x", c_int), ("y", c_int)]
    
    with ThreadPoolExecutor() as executor:
        futures = [executor.submit(POINT, i, i) for i in range(1000)]
        results = [future.result() for future in futures]

    This needs to be both efficient and thread-safe.

    Reacted by Kumar Aditya
  10. kumaraditya303
    added 3 commits that reference this issue on Mar 25, 2025

    gh-127945: add locking to malloc closure in free-threading (#131662)

    96ef4c5

    gh-127945: fix thread safety of ctypes state (#131710)

    f1967e7

    gh-127945: fix thread safety of creating instances of ctypes structur…

    bc5a028
  11. 9 remaining items

  12. kumaraditya303
    added 3 commits that reference this issue on Apr 18, 2025

    gh-127945: skip more tests in ctypes when using parallel threads (#13…

    b9f0943

    gh-127945: add lock held assertions in ctypes arrays (#132720)

    5b06d2c

    gh-127945: add `test_ctypes` to free-threading TSAN CI (#132727)

    8516343
  13. sergey-miryanov commented on May 15, 2025

    @sergey-miryanov
    sergey-miryanov
    on May 15, 2025
    Contributor
    More actions

    Can this issue be closed now as all linked PRs have been merged? Is there anything else that needs to be done?

  14. kumaraditya303
    added a commit that references this issue on May 20, 2025

    gh-127945: Update What's New in Python 3.14 for free-threaded ctypes (#…

    b430e92
  15. kumaraditya303
    closed this as completedin #134332on May 20, 2025
  16. miss-islington
    added a commit that references this issue on May 20, 2025

    pythongh-127945: Update What's New in Python 3.14 for free-threaded c…

    2a74fdc
  17. kumaraditya303
    added a commit that references this issue on May 20, 2025

    [3.14] gh-127945: Update What's New in Python 3.14 for free-threaded …

    6d4b56d
  18. lkollar
    added a commit that references this issue on May 26, 2025

    pythongh-127945: Update What's New in Python 3.14 for free-threaded c…

    483549a
  19. Pranjal095
    added a commit that references this issue on Jul 12, 2025

    pythongh-127945: Update What's New in Python 3.14 for free-threaded c…

    e93ce8a
  20. taegyunkim
    added a commit that references this issue on Aug 4, 2025

    pythongh-127945: Update What's New in Python 3.14 for free-threaded c…

    a7293e0
  21. makslevental
    added 2 commits that reference this issue on Oct 29, 2026

    [MLIR][Python] Fix stale multithreaded_tests.py

    a49de64

    [MLIR][Python] Fix stale multithreaded_tests.py (#237018)

    011983d
  22. llvm-upstreamsync
    added a commit that references this issue on Oct 30, 2026

    Automerge: [MLIR][Python] Fix stale multithreaded_tests.py (#237018)

    eafa8de
  23. llvm-sync
    added a commit that references this issue on Oct 30, 2026

    Automerge: [MLIR][Python] Fix stale multithreaded_tests.py (#237018)

    937e26f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @kumaraditya303
    kumaraditya303

Labels

extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-ctypestopic-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.