Uh oh!
There was an error while loading. Please reload this page.
gh-117657: Make Py_TYPE and Py_SET_TYPE thread safe - #120165
Conversation
colesbury
commented
Jun 6, 2024
Do you have a test / stack trace of the reported race? I don't think we should be modifying an object's type ( |
Example code: classFoo:
passclassBar:
passimportthreadingimportosX=Foo()
defwork():
foo=Xfor_inrange(10000):
foo.__class__=Bartype(foo)
foo.__class__=Footype(foo)
threads= []
foriinrange(os.cpu_count() -1):
thread=threading.Thread(target=work)
thread.start()
threads.append(thread)
work()
forthreadinthreads:
thread.join()TSAN output: Py_SET_TYPE: |
Fidget-Spinner
commented
Jun 7, 2024
Wow... the test case actually causes a segfault even with the fixes, not just a tsan warning. |
vstinner
commented
Jun 7, 2024
Well, Py_TYPE() returns a borrow reference which is a bad thing :-/ But it shouldn't matter in this case, since the type is not deleted by the test. |
Fidget-Spinner
commented
Jun 7, 2024
Can't merge this until we fix the actual crasher code #120198. It seems even default build is affected. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
| } | ||
| Py_BEGIN_CRITICAL_SECTION(self); | ||
| if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) { | ||
| Py_INCREF(newto); |
There was a problem hiding this comment.
Is it important to do the INCREF(newto) and DECREF(oldto) inside the critical section?
Or it it possible to restrict the critical section to the following code?
oldto=Py_TYPE(self);
Py_SET_TYPE(self, newto);There was a problem hiding this comment.
You're right. I can shrink it!
Co-Authored-By: Nadeshiko Manju <me@manjusaka.me>
vstinner
left a comment
There was a problem hiding this comment.
LGTM.
Since my previous review, object_set_class() got a critical section and it LGTM.
Fidget-Spinner
commented
Jun 12, 2024
Thanks for the review Victor! |
Thanks @Fidget-Spinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
GH-120403 is a backport of this pull request to the 3.13 branch. |
colesbury
commented
Jun 17, 2024
I think we should address
The thread-safety guarantees should be something like:
@Fidget-Spinner, would you like to work on this? |
Fidget-Spinner
commented
Jun 17, 2024
@colesbury yeah I can revert the atomics in Py_TYPE and PY_SET_TYPE and stop the world instead. |
…20165) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Nadeshiko Manju <me@manjusaka.me>
…20165) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Nadeshiko Manju <me@manjusaka.me>
…20165) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Nadeshiko Manju <me@manjusaka.me>
Uh oh!
There was an error while loading. Please reload this page.