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 `_warnings.c` thread-safe in free-threaded build · Issue #116664 · python/cpython

Make _warnings.c thread-safe in free-threaded build #116664

New issue
Copy link
New issue
Copy link
Closed
Closed
Make _warnings.c thread-safe in free-threaded build#116664
#117374 (+2)
Copy link
Assignees
erlend-aasland
Labels
topic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement

Description

@colesbury
colesbury
opened on Mar 12, 2024
Issue body actions

Feature or enhancement

The warnings implementation is split between Python (warnings.py) and C (_warnings.c). There are a few bits of code in the C module that are not thread-safe without the GIL:

The Py_SETREF calls are not thread-safe if concurrent writers try to overwrite the same field (e.g., st->once_registry). We can probably use critical sections to make it thread-safe.

get_once_registry:

cpython/Python/_warnings.c

Line 259 in df4784b

Py_SETREF(st->once_registry, registry);

get_default_action:

cpython/Python/_warnings.c

Line 290 in df4784b

Py_SETREF(st->default_action, default_action);

get_filter:

cpython/Python/_warnings.c

Line 315 in df4784b

Py_SETREF(st->filters, warnings_filters);

Some uses of borrowed references are likely not thread-safe

  • _PyDict_GetItemWithError (replace with PyDict_GetItemRef?)

Linked PRs

  • gh-116664: Ensure thread-safe dict access in _warnings #116768
  • gh-116664: Make module state Py_SETREF's in _warnings thread-safe #116959
  • gh-116664: Fix unused var warnings in _warnings.c in non-free-threaded builds #117373
  • gh-116664: In _warnings.c, make filters_version access thread-safe #117374

Activity

  1. colesbury
    added
    type-featureA feature request or enhancementA feature request or enhancement
    topic-free-threading
    on Mar 12, 2024
  2. erlend-aasland commented on Mar 12, 2024

    @erlend-aasland
    erlend-aasland
    on Mar 12, 2024
    Contributor
    More actions

    I'd like to take a stab at this.

    Reacted by Sam Gross and Donghee Na
  3. erlend-aasland
    self-assigned this
    on Mar 12, 2024
  4. erlend-aasland commented on Mar 13, 2024

    @erlend-aasland
    erlend-aasland
    on Mar 13, 2024
    Contributor
    More actions

    FYI: My CPython time is very fragmented these days, so I'll move forward with a handful of smaller PRs instead of one PR that fixes everything.

    Reacted by Donghee Na
  5. erlend-aasland
    added a commit that references this issue on Mar 18, 2024

    gh-116664: Ensure thread-safe dict access in _warnings (#116768)

    762f489
  6. erlend-aasland commented on Mar 18, 2024

    @erlend-aasland
    erlend-aasland
    on Mar 18, 2024
    Contributor
    More actions

    For the module state setref operations: AFAICS all of these happen in warn_explicit()1 helpers, so perhaps we can get away with running the warn_explicit() calls as critical sections.

    Footnotes

    1. the C function, not the Python API ↩

  7. erlend-aasland
    added a commit that references this issue on Mar 18, 2024

    pythongh-116664: Make module state Py_SETREF's in _warnings thread-safe

    42e98b8
  8. vstinner
    added a commit that references this issue on Mar 20, 2024

    pythongh-116664: Ensure thread-safe dict access in _warnings (python#…

    083ad3d
  9. adorilson
    added a commit that references this issue on Mar 25, 2024

    pythongh-116664: Ensure thread-safe dict access in _warnings (python#…

    9bc76f6
  10. erlend-aasland
    linked a pull request that will close this issuegh-116664: Make module state Py_SETREF's in _warnings thread-safe #116959on Mar 28, 2024
  11. erlend-aasland
    linked a pull request that will close this issuegh-116664: Ensure thread-safe dict access in _warnings #116768on Mar 28, 2024
  12. erlend-aasland
    closed this as completedin #116959on Mar 28, 2024
  13. erlend-aasland
    added a commit that references this issue on Mar 28, 2024

    gh-116664: Make module state Py_SETREF's in _warnings thread-safe (#1…

    c1712ef
  14. erlend-aasland commented on Mar 29, 2024

    @erlend-aasland
    erlend-aasland
    on Mar 29, 2024
    Contributor
    More actions

    BTW, don't we need to protect _warnings._filters_mutated?

    cpython/Python/_warnings.c

    Lines 1169 to 1182 in 0fa571d

    warnings_filters_mutated_impl(PyObject *module)
    /*[clinic end generated code: output=8ce517abd12b88f4 input=35ecbf08ee2491b2]*/
    {
    PyInterpreterState *interp = get_current_interp();
    if (interp == NULL) {
    return NULL;
    }
    WarningsState *st = warnings_get_state(interp);
    if (st == NULL) {
    return NULL;
    }
    st->filters_version++;
    Py_RETURN_NONE;
    }

    Possibly also the already_warned(...) calls, since that function reads st->filters_version.

  15. erlend-aasland commented on Mar 29, 2024

    @erlend-aasland
    erlend-aasland
    on Mar 29, 2024
    Contributor
    More actions

    (Reopening; #116959 introduced compilation warnings on non-free-treaded builds.)

  16. erlend-aasland
    reopened this on Mar 29, 2024
  17. colesbury commented on Mar 29, 2024

    @colesbury
    colesbury
    on Mar 29, 2024
    ContributorAuthor
    More actions

    _warnings._filters_mutated: Yes, I think so. Although I think the whole way it's used in catch_warnings is not really thread-safe even with the GIL.

    already_warned(...): I think the calling code already does the locking? It would be good to add an assertion that it's locked at least.

    Reacted by Erlend E. Aasland
  18. erlend-aasland
    added a commit that references this issue on Mar 29, 2024

    pythongh-116664: Fix unused var warnings in _warnings.c in non-free-t…

    e922ada
  19. erlend-aasland commented on Mar 29, 2024

    @erlend-aasland
    erlend-aasland
    on Mar 29, 2024
    Contributor
    More actions

    _warnings._filters_mutated: Yes, I think so. Although I think the whole way it's used in catch_warnings is not really thread-safe even with the GIL.

    👍 BTW, I'm a little bit sceptical about using the @critical_section Argument Clinic directive here, since that uses the module object, and not the mutex in the module state, to lock the critical section. Perhaps it does not matter.

    already_warned(...): I think the calling code already does the locking? It would be good to add an assertion that it's locked at least.

    Yep, an assert would be good.

  20. erlend-aasland
    linked a pull request that will close this issuegh-116664: In _warnings.c, make filters_version access thread-safe #117374on Mar 29, 2024
  21. erlend-aasland
    added a commit that references this issue on Mar 29, 2024

    gh-116664: Fix unused var warnings in _warnings.c in non-free-threade…

    ddf95b5
  22. erlend-aasland
    closed this as completedin #117374on Mar 29, 2024
  23. erlend-aasland
    added a commit that references this issue on Mar 29, 2024

    gh-116664: In _warnings.c, make filters_version access thread-safe (#…

    05e0b67
  24. NGRsoftlab
    added 2 commits that reference this issue on Apr 2, 2024

    Squashed commit of the following:

    1b5fe4c

    Squashed commit of the following:

    a016a54
  25. diegorusso
    added 4 commits that reference this issue on Apr 17, 2024

    pythongh-116664: Ensure thread-safe dict access in _warnings (python#…

    93d77a7

    pythongh-116664: Make module state Py_SETREF's in _warnings thread-sa…

    8250cb6

    pythongh-116664: Fix unused var warnings in _warnings.c in non-free-t…

    5883a40

    pythongh-116664: In _warnings.c, make filters_version access thread-s…

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

Metadata

Metadata

Assignees

  • @erlend-aasland
    erlend-aasland

Labels

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.