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-116738: Make _abc module thread-safe by swtaarrs · Pull Request #117488 · python/cpython

Conversation

@swtaarrs

@swtaarrs swtaarrs commented Apr 2, 2024

Copy link
Copy Markdown
Member

This is a collection of small changes aimed at making the _abc module safe to use in a free-threaded build:

  • Only read and write _abcmodule_state.abc_invalidation_counter and _abc_data._abc_negative_cache_version with atomic operations (except in situations when the object should only be visible to a single thread, like initialization, teardown, or GC traverse).

  • Change the two members above from unsigned long long to uint64_t. This was partially to avoid having to add more _Py_atomic_*_ullong variants, but also because unsigned long long is guaranteed to be at least 64 bits and I can't imagine we'd ever want more than 64. Might as well make it explicit.

  • Change _in_weak_set() and _add_to_weak_set() to both take an _abc_data * and PyObject **, to allow them to use critical sections when reading or writing the pointers to sets of weak references. None of the PyObject *s that hold a set will change once they're first initialized, so we don't need to use locking when operating on the sets, only when reading or initializing the pointers.

  • For the most part, no locks are held around multiple operations on related data structures (_abc__abc_subclasscheck_impl() being a good example). User code that does things like performing ABC subclass checks while concurrently registering subclasses will always be subject to surprising results no matter what we do internally, so the module only ensures that its data structures are kept internally consistent.

  • Two functions modify a type's tp_flags member: _abc__abc_init() (should only be called with new types not visible to other threads) and _abc__abc_register_impl() (called with existing types). I added a helper to typeobject.c to support the _abc_register case, and it was just a few more lines to also support the _abc_init case as well. This felt a bit heavy-handed so I'm open to suggestions.

  • Issue: Audit all built-in modules for thread safety #116738

Sorry, something went wrong.

Uh oh!

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

All reactions
@swtaarrs
Make _abc module thread-safe
16de100
@swtaarrs swtaarrs added skip news extension-modules C modules in the Modules dir topic-free-threading labels Apr 2, 2024
@swtaarrs
swtaarrs requested a review from colesbury April 2, 2024 23:09
@bedevere-app bedevere-app Bot mentioned this pull request Apr 2, 2024
Audit all built-in modules for thread safety #116738
Open
@swtaarrs
swtaarrs marked this pull request as ready for review April 3, 2024 18:30
@swtaarrs
swtaarrs requested a review from markshannon as a code owner April 3, 2024 18:30
@bedevere-app bedevere-app Bot added the awaiting review label Apr 3, 2024
colesbury
colesbury reviewed Apr 10, 2024
View reviewed changes

@colesbury colesbury left a comment

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.

Overall looks good. A few comments below.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Modules/_abc.c Resolved

Uh oh!

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

Comment thread Modules/_abc.c Resolved

Uh oh!

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

@swtaarrs
Review comments from Sam
a117aac
colesbury
colesbury approved these changes Apr 11, 2024
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Apr 11, 2024
@swtaarrs

swtaarrs commented Apr 11, 2024

Copy link
Copy Markdown
Member Author

Don't merge this yet - I'm still going to make a small improvement to the refcounting of flags (hopefully after my next meeting)

colesbury reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

swtaarrs added 2 commits April 11, 2024 14:22
@swtaarrs
Use PyDict_Pop() and further simplify
77464cc
@swtaarrs
Merge remote-tracking branch 'upstream/main' into cpython-_abc-module
58a607a
@swtaarrs

swtaarrs commented Apr 11, 2024

Copy link
Copy Markdown
Member Author

Ok, this should be good to go once the builds finish.

All reactions

Sorry, something went wrong.

Uh oh!

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

@colesbury
colesbury merged commit f268e32 into python:main Apr 11, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label Apr 11, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
@swtaarrs @diegorusso
pythongh-116738: Make _abc module thread-safe (python#117488)
4212e01
A collection of small changes aimed at making the `_abc` module safe to
use in a free-threaded build.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@colesbury colesbury colesbury approved these changes
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

extension-modules C modules in the Modules dir skip news topic-free-threading

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.

2 participants

@swtaarrs @colesbury
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.