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 — Data race in `Decimal.__hash__()` · Issue #154037 · python/cpython

Data race in Decimal.__hash__() #154037

New issue
Copy link
New issue
Copy link
Closed
Closed
Data race in Decimal.__hash__()#154037
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@skirpichev
skirpichev
opened on Jul 18, 2026
Issue body actions

Bug report

Bug description:

Decimal.__hash__() writes its lazy hash cache (self->hash) with plain stores (-1 default). On a free-threaded build, two threads calling hash() on the same shared Decimal race on the cache field - one reads the -1 sentinel while another stores the computed hash. hash() looks read-only to callers, so a shared Decimal is not safe to hash concurrently.

This is a sub-issue of #153852, see gist here.

Reproducer:

import sys, threading
from decimal import Decimal
assert not sys._is_gil_enabled(), "run free-threaded: PYTHON_GIL=0"

NT = 4
ROUNDS = 4000
pool = [None]
enter = threading.Barrier(NT + 1)
leave = threading.Barrier(NT + 1)

def worker():
    for _ in range(ROUNDS):
        enter.wait()
        for d in pool[0]:
            hash(d)                     # dec_hash: read self->hash==-1, then write it
        leave.wait()

ts = [threading.Thread(target=worker) for _ in range(NT)]
for t in ts: t.start()
for r in range(ROUNDS):
    pool[0] = [Decimal(f"{r}.{i}") for i in range(64)]   # fresh, unhashed each round
    enter.wait(); leave.wait()
for t in ts: t.join()
print("done, no crash")

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

  • gh-154037: Fix race in the Decimal.__hash__() #154045

Activity

  1. skirpichev
    self-assigned this
    on Jul 18, 2026
  2. skirpichev
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    topic-free-threading
    on Jul 18, 2026
  3. skirpichev
    added a parent issue on Jul 18, 2026
    • Free-threading data races in CPython: 15 findings (9 new + 6 residual of existing FT work) #153852
  4. skirpichev
    removed their assignment
    on Jul 19, 2026
  5. terryjreedy
    changed the title [-]Data race in Decimal.__hash__()[/-] [+]Data race in `Decimal.__hash__()`[/+] on Jul 19, 2026
  6. kumaraditya303
    added a commit that references this issue on Jul 30, 2026

    gh-154037: Fix race in the hash(Decimal) (#154045)

    2f381f5
  7. kumaraditya303
    closed this as completedon Jul 30, 2026
  8. mbeijen
    added a commit that references this issue on Aug 14, 2026

    pythongh-154037: Fix race in the hash(Decimal) (python#154045)

    c367fd1
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

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