Skip to content

gh-128182: Switch ctypes to solely critical sections#132133

Merged
kumaraditya303 merged 11 commits into
python:mainfrom
ZeroIntensity:ctypes-critical-sections
Apr 7, 2025
Merged

gh-128182: Switch ctypes to solely critical sections#132133
kumaraditya303 merged 11 commits into
python:mainfrom
ZeroIntensity:ctypes-critical-sections

Conversation

@ZeroIntensity

@ZeroIntensity ZeroIntensity commented Apr 5, 2025

Copy link
Copy Markdown
Member

Removes the LOCK_PTR/UNLOCK_PTR macros, and fixes two races related to missing locks.

Comment thread Modules/_ctypes/_ctypes.c Outdated
} else {
LOCK_PTR(self);
Py_BEGIN_CRITICAL_SECTION(self);
*((void **)self->b_ptr) = dst->b_ptr;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain again why we can't use locked_deref_assign here? is it because of pointer rules? (because here, we're just re-assigning the stuff right?)

@kumaraditya303 kumaraditya303 Apr 6, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to get rid of locked_deref_assign in future so using it less is better. Also that reacquires the critical sections so will cause perf loss

Comment thread Modules/_ctypes/_ctypes.c
if (err) {
locked_memcpy_from(ptr, src, size);
Py_BEGIN_CRITICAL_SECTION(src);
memcpy(ptr, src->b_ptr, size);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the critical section should be held longer? because GetKeepedObjects() is calling PyCData_GetContainer() which itself uses Py_BEGIN_CRITICAL_SECTION(src), so aren't we having a re-entrant critical section lock?

@ZeroIntensity ZeroIntensity Apr 7, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, which critical sections are allowed to do 😄

We can't release the lock before calling GetKeepedObjects, but I guess we could call GetKeepedObjects_lock_held. Edit: nevermind, that's not a real function.

@kumaraditya303

Copy link
Copy Markdown
Contributor

The existing usage of locked_memcpy are not thread safe and needs to be reverted back to manual memcpy with holding the critical section.

For example, on this line it acquires the critical section after reading the self->b_size so it is not safe.

locked_memcpy_from(&parg->value, self, self->b_size);

@ZeroIntensity

Copy link
Copy Markdown
Member Author

Yeah, I'll remove that too.

@python-cla-bot

python-cla-bot Bot commented Apr 6, 2025

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@kumaraditya303 kumaraditya303 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are merge conflicts

@bedevere-app

bedevere-app Bot commented Apr 7, 2025

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@ZeroIntensity

Copy link
Copy Markdown
Member Author

Got rid of locked_memcpy* and locked_deref_assign. That should fix the rest of the thread safety issues here, but we might need to take one extra look at _PyCData_set.

I didn't expect the Spanish Inquisition.

@bedevere-app

bedevere-app Bot commented Apr 7, 2025

Copy link
Copy Markdown

Nobody expects the Spanish Inquisition!

@kumaraditya303: please review the changes made to this pull request.

@bedevere-app bedevere-app Bot requested a review from kumaraditya303 April 7, 2025 11:24
Comment thread Modules/_ctypes/_ctypes.c Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants