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-112075: Use atomic exchange in Py_SETREF so updating dict pointers doesn't race by DinoV · Pull Request #116620 · python/cpython

Conversation

@DinoV

@DinoV DinoV commented Mar 11, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Updating the dict for an object uses Py_SETREF. This can race if multiple threads are attempting to do this and dec ref the original value multiple times. This uses _Py_atomic_exchange_ptr to update the existing value and get the previous value for a dec ref.

  • Issue: Make dict objects thread-safe in --disable-gil builds #112075

Sorry, something went wrong.

Uh oh!

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

All reactions
@DinoV
Use atomic exchange in Py_SETREF
f5892e7
@DinoV DinoV added skip news topic-free-threading labels Mar 11, 2024
@DinoV
DinoV requested a review from colesbury March 11, 2024 21:22
@bedevere-app bedevere-app Bot mentioned this pull request Mar 11, 2024
Make dict objects thread-safe in --disable-gil builds #112075
Closed
@colesbury

colesbury commented Mar 11, 2024

Copy link
Copy Markdown
Contributor

This changes every Py_SETREF to use an atomic exchange, not just for updating dict pointers. That seems too expensive

All reactions

Sorry, something went wrong.

Uh oh!

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

@DinoV
DinoV marked this pull request as ready for review March 12, 2024 00:57
@bedevere-app bedevere-app Bot added the awaiting core review label Mar 12, 2024
@DinoV

DinoV commented Mar 12, 2024

Copy link
Copy Markdown
Contributor Author

This changes every Py_SETREF to use an atomic exchange, not just for updating dict pointers. That seems too expensive

It seems like having this be thread-safe by default and providing a way to opt out of it would be best? There's something like 280 references to this and it seems like around 100 of those are storing into memory which is likely to be shared - and that's just in CPython.

All reactions

Sorry, something went wrong.

Uh oh!

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

@colesbury

colesbury commented Mar 12, 2024

Copy link
Copy Markdown
Contributor

Making Py_SETREF atomic is not generally sufficient because you still have a race between other loads and the Py_DECREF inside Py_SETREF. For updating dict pointers, we may delay deallocation with QSBR, but that's not a strategy we expect extensions to use. More commonly, we want updates and reads to be protected by a critical section.

I randomly sampled 10 out of the 262 usages of Py_SETREF in C files (excluding Docs/). Two were unsafe (in _warnings.c) and those need additional synchronization. Six were Py_SETREF on local variables and two more were safe for more subtle reasons (updating thread-local state and modification in tp_clear).

Sampled call sites
mathmodule.c `Py_SETREF(total, new_total);`
64: local

stringio.c `Py_SETREF(decoded, translated);`
104: local

bytesobject.c `Py_SETREF(bytes, bytes_subtype_new(type, bytes));`
133: local

floatobject.c `Py_SETREF(result, PyObject_CallOneArg((PyObject *)type, result));`
147: local

funcobject.c: `Py_SETREF(op->func_qualname, &_Py_STR(empty));`
153: shared, but safe (in tp_clear)

longobject.c `Py_SETREF(a, n)`
164: local

_warnings.c: `Py_SETREF(st->once_registry, registry)`
222: shared (unsafe)

_warnings.c: `Py_SETREF(st->filters, warnings_filters)`
224: shared (unsafe)

context.c `Py_SETREF(ctx->ctx_vars, new_vars);`
241: thread-local

import.c: `Py_SETREF(package, substr)`
255: local
All reactions

Sorry, something went wrong.

Uh oh!

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

@DinoV DinoV closed this Apr 19, 2024
@DinoV
DinoV deleted the nogil_dict_setref branch May 31, 2024 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@colesbury colesbury Awaiting requested review from colesbury

Uh oh!

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

Assignees

No one assigned

Labels

awaiting core review 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

@DinoV @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.