-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-146270: Fix PyMember_SetOne(..., NULL) not being atomic
#148800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dpdani
wants to merge
6
commits into
python:main
Choose a base branch
from
dpdani:gh/146270-fix-slot-del-atomic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
98cbbc0
gh-146270: Fix `PyMember_SetOne(..., NULL)` not being atomic
dpdani 7a7114b
📜🤖 Added by blurb_it.
blurb-it[bot] 47b58c9
fix backticks
dpdani 015795a
address some review comments
dpdani 50c19f6
decrease number of iterations
dpdani e036ecc
change test and remove spinningbarrier
dpdani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core_and_Builtins/2026-04-20-15-25-55.gh-issue-146270.qZYfyc.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix a sequential consistency bug in ``structmember.c``. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,19 +171,10 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v) | |
| PyErr_SetString(PyExc_AttributeError, "readonly attribute"); | ||
| return -1; | ||
| } | ||
| if (v == NULL) { | ||
| if (l->type == Py_T_OBJECT_EX) { | ||
| /* Check if the attribute is set. */ | ||
| if (*(PyObject **)addr == NULL) { | ||
| PyErr_SetString(PyExc_AttributeError, l->name); | ||
| return -1; | ||
| } | ||
| } | ||
| else if (l->type != _Py_T_OBJECT) { | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "can't delete numeric/char attribute"); | ||
| return -1; | ||
| } | ||
| if (v == NULL && l->type != Py_T_OBJECT_EX && l->type != _Py_T_OBJECT) { | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "can't delete numeric/char attribute"); | ||
| return -1; | ||
| } | ||
| switch (l->type) { | ||
| case Py_T_BOOL:{ | ||
|
|
@@ -334,6 +325,15 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v) | |
| oldv = *(PyObject **)addr; | ||
| FT_ATOMIC_STORE_PTR_RELEASE(*(PyObject **)addr, Py_XNewRef(v)); | ||
| Py_END_CRITICAL_SECTION(); | ||
| if (v == NULL && oldv == NULL && l->type == Py_T_OBJECT_EX) { | ||
| // Raise an exception when attempting to delete an already deleted | ||
| // attribute. | ||
| // Differently from Py_T_OBJECT_EX, _Py_T_OBJECT does not raise an | ||
| // exception here (PyMember_GetOne will return Py_None instead of | ||
| // NULL). | ||
| PyErr_SetString(PyExc_AttributeError, l->name); | ||
| return -1; | ||
| } | ||
|
Comment on lines
+328
to
+336
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
if (v == NULL && oldv == NULL && l->type == Py_T_OBJECT_EX) {
// Raise an exception when attempting to delete an already deleted attribute
PyErr_SetString(PyExc_AttributeError, l->name);
return -1;
}
Py_XDECREF(oldv);
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't understand what you meant with "The l->type is redundant." I have expanded a comment, hopefully it clarifies why that check is needed? |
||
| Py_XDECREF(oldv); | ||
| break; | ||
| case Py_T_CHAR: { | ||
|
|
||
Oops, something went wrong.
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.
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 test is too slow. It's not worth trying to catch every sort non-sequential consistency. If you catch the data race under TSan reasonably often, that's fine:
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.
When you say it's too slow, what upper bound do you have in mind?
Btw, with your test, I cannot get it to fail when reverting
structmember.c, nor did I get TSan warnings in 5 out of 5 attempts.I think the reason why that's so is that just starting threads doesn't generate enough contention: they're not actually going to hit the current
*(PyObject **)addr == NULLat line 177 concurrently with theFT_ATOMIC_STORE_PTR_RELEASEat line 335. This is why I addedSpinningBarrier, to generate more contention and have them hit these lines concurrently, in a reliable way.I can also just reduce the number of iterations in my test. Even with just
iters = 10, TSan reported a race 10 out of 10 times. I kinda put 1000 to begin with and then forgot about it.With
iters = 10, the whole test suite runs in about 35ms on my MacBook, albeit the test doesn't fail reliably with TSan turned off, only failing ~33% of the times.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.
You should aim for <100ms. I get a TSan error on nearly every run
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.
With the current test on 10 iterations, the whole suite runs in <50ms, so I think that should be fine.
It's curious that you're getting more TSan errors, on my macbook I'm seeing 1 TSan warning every ~20 runs. Can I ask you what's your
./configure? Mine is./configure --config-cache --disable-gil --with-pydebug --with-thread-sanitizer.Btw, if I use your test code and switch
threading.Barrier->_testcapi.SpinningBarrierinsiderun_concurrently, I saw a TSan warning on 100 out of 100 runs.To be clear, I'm not suggesting to use SpinningBarrier in
run_concurrently, I'm saying that it definitely gives more determinism to the results, and I think it's worth keeping it.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.
I think configuring
--with-pydebugwas what caused my warnings to trigger much less, I have applied your test and removed the spinning barrier.