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 — `arraymodule`, `_decimal`: possible lazy import caches reference leaks in free-threaded builds · Issue #149006 · python/cpython

arraymodule, _decimal: possible lazy import caches reference leaks in free-threaded builds #149006

New issue
Copy link
New issue
Copy link
Open
Open
arraymodule, _decimal: possible lazy import caches reference leaks in free-threaded builds#149006
Copy link
Labels
topic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@maurycy
maurycy
opened on Apr 26, 2026
Issue body actions

Bug description:

As pointed out by @htjworld in #148088 (comment):

I was wondering about the lazy init pattern in a free-threaded build. If two threads enter build_struct_time concurrently with st->time_struct_time == NULL, both would call PyImport_ImportModuleAttrString and one overwrite would leave a reference with an incremented refcount that never gets decref'd.

I noticed arraymodule.c and _decimal.c have the same pattern, so maybe this is just accepted. But _asynciomodule.c initializes eagerly in module_init() which avoids this structurally. Was lazy chosen deliberately here over eager init in init_state()?

While I've addressed this in #148088, I think that this is a valid concern, and import caches should use CAS, just like:

cpython/Modules/_sre/sre.c

Lines 1173 to 1188 in 7e0a0be

PyObject *func = FT_ATOMIC_LOAD_PTR(module_state->compile_template);
if (func == NULL) {
func = PyImport_ImportModuleAttrString("re", "_compile_template");
if (func == NULL) {
return NULL;
}
#ifdef Py_GIL_DISABLED
PyObject *other_func = NULL;
if (!_Py_atomic_compare_exchange_ptr(&module_state->compile_template, &other_func, func)) {
Py_DECREF(func);
func = other_func;
}
#else
Py_XSETREF(module_state->compile_template, func);
#endif
}

If there's interest, I can do it.

(It seems similar to #149000, so perhaps a more thorough check warranted.)

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Reacted by htjworld

Activity

  1. maurycy
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 26, 2026
  2. maurycy
    changed the title [-]Possible lazy import caches reference leaks in free-threaded builds[/-] [+]`arraymodule`, `_decimal`: possible lazy import caches reference leaks in free-threaded builds[/+] on Apr 26, 2026
  3. htjworld commented on Apr 26, 2026

    @htjworld
    htjworld
    on Apr 26, 2026
    Contributor
    More actions

    Hi @maurycy — interested in helping with this one. Happy to follow your lead since you opened it, but a couple of things I could pick up:

    • Take a first pass at the CAS fix for arraymodule.c or _decimal.c, whichever you're not already on
    • Grep around for other sites with the same pattern and post a checklist here

    Or if you'd rather drive both, happy to just review. Let me know what works.

  4. maurycy commented on Apr 26, 2026

    @maurycy
    maurycy
    on Apr 26, 2026
    ContributorAuthor
    More actions

    Hi @maurycy — interested in helping with this one. Happy to follow your lead since you opened it, but a couple of things I could pick up:

    • Take a first pass at the CAS fix for arraymodule.c or _decimal.c, whichever you're not already on
    • Grep around for other sites with the same pattern and post a checklist here

    Or if you'd rather drive both, happy to just review. Let me know what works.

    I'm not a core dev! Let's wait for a green light from the higher ups :-)

    Reacted by htjworld
  5. serhiy-storchaka
    added
    topic-free-threading
    on Jul 3, 2026
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

    topic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    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.