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 — `threading.local()` implementation is not thread-safe in the free-threaded build · Issue #120973 · python/cpython

threading.local() implementation is not thread-safe in the free-threaded build #120973

New issue
Copy link
New issue
Copy link
Closed
Closed
threading.local() implementation is not thread-safe in the free-threaded build#120973
Copy link
Assignees
mpage
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixestopic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@colesbury
colesbury
opened on Jun 24, 2024
Issue body actions

Bug report

The implementation of Python thread local variables (threading.local() or _thread._local) has some thread-safety issues. The issues are particularly relevant to the free-threaded build, but some can affect the default build too.

local_clear loop over threads isn't safe

The local_clear() function is called when a thread local variable is destroyed. It loops over all threads in the interpreter and removes itself from their dictionaries.

cpython/Modules/_threadmodule.c

Lines 1568 to 1581 in fd0f814

HEAD_LOCK(runtime);
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
HEAD_UNLOCK(runtime);
while (tstate) {
if (tstate->dict) {
if (PyDict_Pop(tstate->dict, self->key, NULL) < 0) {
// Silently ignore error
PyErr_Clear();
}
}
HEAD_LOCK(runtime);
tstate = PyThreadState_Next(tstate);
HEAD_UNLOCK(runtime);
}

This isn't thread-safe because after HEAD_UNLOCK(runtime), the stored tstate might be deleted concurrently. This can happen even in the default build with the GIL enabled because PyThreadState_Delete() doesn't require the GIL to be held. However, it's less likely to occur in practice because threading module created threads hold onto the GIL until they're deleted.

local_clear access to tstate->dict isn't thread-safe

In the free-threaded build, local_clear() may be run concurrently with some other thread's PyThreadState_Clear(). The access to another thread's tstate->dict isn't safe because it may be getting destroyed concurrently.

Linked PRs

  • gh-120973: Fix thread-safety issues with threading.local #121655
  • [3.13] gh-120973: Fix thread-safety issues with threading.local (GH-121655) #122042

Activity

  1. colesbury
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    3.13only security fixesonly security fixes
    topic-free-threading
    3.14bugs and security fixesbugs and security fixes
    on Jun 24, 2024
  2. mpage
    self-assigned this
    on Jun 28, 2024
  3. corona10 commented on Jun 29, 2024

    @corona10
    corona10
    on Jun 29, 2024
    Member
    More actions

    @mpage #118490 if the linked issue is related to this issue, please take a look at both of them :)

    Reacted by mpage
  4. mpage
    added a commit that references this issue on Jul 16, 2024

    Merge branch 'main' into pythongh-120973-fix-local-clear

    0e9a0bc
  5. colesbury
    added 2 commits that reference this issue on Jul 19, 2024

    Merge branch 'main' into pythongh-120973-fix-local-clear

    9a7350d

    gh-120973: Fix thread-safety issues with `threading.local` (#121655)

    e059aa6
  6. miss-islington
    added a commit that references this issue on Jul 19, 2024

    pythongh-120973: Fix thread-safety issues with `threading.local` (pyt…

    1204d28
  7. colesbury
    added a commit that references this issue on Jul 19, 2024

    [3.13] gh-120973: Fix thread-safety issues with `threading.local` (GH…

    bf7f4f3
  8. colesbury
    closed this as completedon Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @mpage
    mpage

Labels

3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixestopic-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.