Add locking to refnanny.
Add mutex to guard the three exception state variables in a parallel block.
Untested.
"Fixes" #6198
https://github.com/cython/cython/pull/6199
(5 files)
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
@scoder commented on this pull request.
Looks good to me.
> + c.putln("#else")
+ c.putln(f"int {Naming.parallel_freethreading_mutex}=0;")
+ c.putln(f"(void){Naming.parallel_freethreading_mutex};")
This seems unused. All uses of the lock variable are guarded by the …_NOGIL define.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
@da-woods commented on this pull request.
> + c.putln("#else")
+ c.putln(f"int {Naming.parallel_freethreading_mutex}=0;")
+ c.putln(f"(void){Naming.parallel_freethreading_mutex};")
It's declared as shared in the openmp pragma (and I'm not sure there's an easy way of guarding that) so I think this is used.
shared_vars.append(Naming.parallel_freethreading_mutex)
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
I'll wait until either I've had time to set up a free-threading build and give it a try, or until someone with one reports back.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
@lysnikolaou commented on this pull request.
A couple of initial comments. I'm currently trying to test this.
> @@ -9767,6 +9772,12 @@ def end_parallel_control_flow_block(
if self.error_label_used:
c.putln("const char *%s = NULL; int %s = 0, %s = 0;" % self.parallel_pos_info)
c.putln("PyObject *%s = NULL, *%s = NULL, *%s = NULL;" % self.parallel_exc)
+ c.putln("#if CYTHON_COMPILING_IN_CPYTHON_NOGIL")
+ c.putln(f"PyMutex {Naming.parallel_freethreading_mutex};")
We need to initialize the mutex here, right?
⬇️ Suggested change- c.putln(f"PyMutex {Naming.parallel_freethreading_mutex};")
+ c.putln(f"PyMutex {Naming.parallel_freethreading_mutex} = {0};")
In Cython/Runtime/refnanny.pyx:
> + #if CYTHON_COMPILING_IN_CPYTHON_NOGIL
+ static CYTHON_INLINE int __Pyx_refnanny_init_lock(PyThread_type_lock* lock) {
+ *lock = PyThread_allocate_lock();
+ if (!*lock) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ return 0;
+ }
+
+ static CYTHON_INLINE void __Pyx_refnanny_free_lock(PyThread_type_lock lock) {
+ PyThread_free_lock(lock);
+ }
+
+ static CYTHON_INLINE void __Pyx_refnanny_lock_acquire(PyThread_type_lock lock) {
+ while (!PyThread_acquire_lock_timed(lock, 100, 0) {
⬇️ Suggested change
- while (!PyThread_acquire_lock_timed(lock, 100, 0) {
+ while (!PyThread_acquire_lock_timed(lock, 100, 0)) {
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
@da-woods pushed 1 commit.
—
View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
@lysnikolaou commented on this pull request.
> @@ -9767,6 +9772,12 @@ def end_parallel_control_flow_block(
if self.error_label_used:
c.putln("const char *%s = NULL; int %s = 0, %s = 0;" % self.parallel_pos_info)
c.putln("PyObject *%s = NULL, *%s = NULL, *%s = NULL;" % self.parallel_exc)
+ c.putln("#if CYTHON_COMPILING_IN_CPYTHON_NOGIL")
+ c.putln(f"PyMutex {Naming.parallel_freethreading_mutex} = {0};")
I forgot this was inside an f-string before. I'm sorry about that.
⬇️ Suggested change- c.putln(f"PyMutex {Naming.parallel_freethreading_mutex} = {0};")
+ c.putln(f"PyMutex {Naming.parallel_freethreading_mutex} = {{0}};")
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
@da-woods pushed 1 commit.
—
View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
I'll merge this in a couple of days if there aren't any further comments. I wasn't able to reproduce the bug it was allegedly supposed to be fixing. But I think we do want locking in these places, and I was able to test the PR and confirm that it doesn't seem to break anything.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
FWIW I agree we can merge this and test more intensively later, it certainly won't make things worse.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()