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 — gh-154431: Use existing `runtime->audit_hooks.mutex` instead of a new one by sobolevn · Pull Request #154494 · python/cpython

Conversation

@sobolevn

@sobolevn sobolevn commented Jul 22, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: Data race: sys.addaudithook() lazily creates interp->audit_hooks with no lock #154431

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sobolevn
pythongh-154431: Use existing runtime->audit_hooks.mutex instead of…
8adcde5
… a new one
@sobolevn
sobolevn requested a review from ambv July 22, 2026 16:30
@sobolevn
sobolevn requested review from FFY00, ZeroIntensity and ericsnowcurrently as code owners July 22, 2026 16:30
@bedevere-app bedevere-app Bot mentioned this pull request Jul 22, 2026
Data race: sys.addaudithook() lazily creates interp->audit_hooks with no lock #154431
Open
@bedevere-app bedevere-app Bot added the awaiting core review label Jul 22, 2026
ZeroIntensity
ZeroIntensity reviewed Jul 22, 2026
View reviewed changes

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will just increase contention in sys.addaudithook, won't it?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sobolevn

sobolevn commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

I don't think that we actually concurrenly add hooks with both PySys_AddAuditHook and sys.audithook, so realistically - it should not matter.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn sobolevn added skip news needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Jul 22, 2026
@ZeroIntensity

ZeroIntensity commented Jul 22, 2026

Copy link
Copy Markdown
Member

Agreed, but why make this change at all? We're just increasing pressure on a lock for no reason. PyMutex is just a single byte; due to alignment, I would be surprised if you see any change in memory overhead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn

sobolevn commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

Because we can't backport the fix with interpreter state changed due to ABI change.
And we want to fix the data race on 3.14 :(

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

weixlu
weixlu reviewed Jul 23, 2026
View reviewed changes
Comment thread Python/sysmodule.c

PyInterpreterState *interp = tstate->interp;
PyMutex mutex = interp->audit_hooks_mutex;
PyMutex mutex = interp->runtime->audit_hooks.mutex;

@weixlu weixlu Jul 23, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I noticed the mutex here is copied by value onto the stack, then being locked. I'm worried this subtly changes the semantics into locking a stack-local object instead. The typical usage may be to lock the address directly, i.e. PyMutex_Lock(&interp->runtime->audit_hooks.mutex);

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity and encukou reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

@weixlu weixlu Jul 23, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a demo shows locking a stack-local mutex won't affect the real mutex.

#include <Python.h>
#include <stdio.h>

static PyMutex shared = {0};

int main(void)
{
    PyMutex mutex = shared;
    PyMutex_Lock(&mutex);
    printf("locked the COPY:  shared=%d copy=%d\n", PyMutex_IsLocked(&shared), PyMutex_IsLocked(&mutex));
    PyMutex_Unlock(&mutex);

    PyMutex_Lock(&shared);
    printf("locked the FIELD: shared=%d\n", PyMutex_IsLocked(&shared));
    PyMutex_Unlock(&shared);
    return 0;
}

Run gcc mutex_copy_demo.c -I Include -I . libpython3.16.a -lpthread -lm -ldl -lutil -o demo && ./demo then it print

locked the COPY:  shared=0 copy=1
locked the FIELD: shared=1

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sobolevn reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@sobolevn sobolevn mentioned this pull request Jul 23, 2026
gh-154431: Fix data race in sys.audithook #154462
Merged
@ZeroIntensity ZeroIntensity mentioned this pull request Jul 23, 2026
gh-154431: Eagerly initialize the audit hooks list #154554
Open
@encukou

encukou commented Jul 27, 2026

Copy link
Copy Markdown
Member

!buildbot CentOS9.NoGIL.Refleaks

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Jul 27, 2026

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @encukou for commit 8adcde5 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F154494%2Fmerge

The command will test the builders whose names match following regular expression: CentOS9.NoGIL.Refleaks

The builders matched are:

  • AMD64 CentOS9 NoGIL Refleaks PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Oct 28, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ZeroIntensity ZeroIntensity ZeroIntensity left review comments
@ambv ambv Awaiting requested review from ambv
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@FFY00 FFY00 Awaiting requested review from FFY00 FFY00 is a code owner
+1 more reviewer
@weixlu weixlu weixlu left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes skip news stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@sobolevn @ZeroIntensity @encukou @bedevere-bot @weixlu
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.