Skip to content

Data race: sys.addaudithook() lazily creates interp->audit_hooks with no lock #154431

Description

@sobolevn

Bug report

Full report: https://github.com/proxy/gist.github.com/devdanzin/0b13838fd6089e73a3f063ed8f68e733

Repro:

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

NADD = 24      # threads slamming the first-time lazy-init store (write @540) at once
NAUD = 8       # threads spinning audit events (should_audit read @239)
barrier = threading.Barrier(NADD + NAUD)

def _hook(*a):
    return None

def adder():
    barrier.wait()
    for _ in range(200):
        sys.addaudithook(_hook)          # write interp->audit_hooks (first time) @540

def auditor():
    barrier.wait()
    for _ in range(200000):
        sys.audit("fusil.tsan.test")     # should_audit read of interp->audit_hooks @239

ts = [threading.Thread(target=adder) for _ in range(NADD)]
ts += [threading.Thread(target=auditor) for _ in range(NAUD)]
for t in ts: t.start()
for t in ts: t.join()
print("done, no crash")

The fix is incoming.

Linked PRs

Activity

  1. self-assigned this
    on Jul 22, 2026
  2. added a commit that references this issue on Jul 22, 2026
  3. added a commit that references this issue on Jul 22, 2026
  4. added a commit that references this issue on Jul 22, 2026
  5. added a commit that references this issue on Jul 27, 2026
  6. encukou commented on Jul 27, 2026

    @encukou
    Member

    The commit caused a refleak that made tier 1 buildbot fail (e.g. [here](e.g. https://buildbot.python.org/#/builders/1610/builds/3726)); it was failing throughout the weekend. I've reverted to get the CI green.
    Please add the change itself (an un-revert) to the follow-up PR(s). Let me know if you want assistance.

  7. added a commit that references this issue on Jul 27, 2026
  8. added a commit that references this issue on Jul 27, 2026
  9. added a commit that references this issue on Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions