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
commented
Mar 11, 2024
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
This changes every Py_SETREF to use an atomic exchange, not just for updating dict pointers. That seems too expensive
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
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
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
colesbury
Awaiting requested review from colesbury
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
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.
|