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 — `faulthandler` itself crashes in free-threading build (in `_Py_DumpExtensionModules`) · Issue #120837 · python/cpython

faulthandler itself crashes in free-threading build (in _Py_DumpExtensionModules) #120837

New issue
Copy link
New issue
Copy link
Closed
Closed
faulthandler itself crashes in free-threading build (in _Py_DumpExtensionModules)#120837
Copy link
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 21, 2024
Issue body actions

Bug report

The faulthandler module can dump Python tracebacks when a crash occurs. Unfortunately, the current implementation itself crashes in the free-threaded build. This is mostly undetected because our tests expect a crash, but faulthandler itself crashing is not desirable.

Faulthandler may be called without a valid thread state (i.e., without holding GIL)

Faulthandler may be triggered when the thread doesn't have a valid thread state (i.e., doesn't hold the GIL in the default build and is not "attached" in the free-threaded build). Additionally, it's called from a signal handler, so we only want to call async-signal-safe functions (generally no locking).

Faulthandler calls PyDict_Next (via _Py_DumpExtensionModules) on the modules dictionary. This is not entirely safe in the default build (because we don't hold the GIL), but works well enough in practice.

However, it will consistently crash in the free-threaded build because PyDict_Next starts a critical section, which assumes there is a valid thread state.

Suggestion:

  • we should use _PyDict_Next(), which doesn't internally lock the dict
  • we should try to lock the dict around the _PyDict_Next() loop, with _PyMutex_LockTimed timeout=0. If we can't immediately lock the dict, we should not dump modules. This async-signal-safe because it's just a simple compare-exchange and doesn't block.
  • we can't call PyMutex_Unlock() because it's not async-signal-safe (it internally acquires locks in order to wake up threads), so we should either use a simple atomic exchange to unlock the dict (without waking up waiters) or not bother unlocking the lock at all. We exit shortly after _Py_DumpExtensionModules, so it doesn't matter if we don't wake up other threads.

Linked PRs

  • gh-120837: Update _Py_DumpExtensionModules to be async-signal-safe #121051
  • [3.13] gh-120837: Update _Py_DumpExtensionModules to be async-signal-safe (gh-121051) #121107

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 21, 2024
  2. corona10
    added 2 commits that reference this issue on Jun 26, 2024

    pythongh-120837: Update _Py_DumpExtensionModules to be async-signal-safe

    8430508

    gh-120837: Update _Py_DumpExtensionModules to be async-signal-safe (g…

    1a2e7a7
  3. miss-islington
    added a commit that references this issue on Jun 27, 2024

    pythongh-120837: Update _Py_DumpExtensionModules to be async-signal-s…

    eca340e
  4. corona10
    added a commit that references this issue on Jun 27, 2024

    [3.13] gh-120837: Update _Py_DumpExtensionModules to be async-signal-…

    d5441f6
  5. corona10
    closed this as completedon Jun 27, 2024
  6. mrahtz
    added a commit that references this issue on Jun 30, 2024

    pythongh-120837: Update _Py_DumpExtensionModules to be async-signal-s…

    91a1a8b
  7. estyxx
    added a commit that references this issue on Jul 17, 2024

    pythongh-120837: Update _Py_DumpExtensionModules to be async-signal-s…

    4c103de
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

    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.