Skip to content

Conversation

@gaogaotiantian
Copy link
Member

@gaogaotiantiangaogaotiantian commented Feb 20, 2025

In #127272 a PyCell_Get usage in frameobject.c was missed from being replaced for free-threaded safety. I guess the reason was probably the function was labelled to return a borrowed reference and a deeper understanding of the piece of code is needed to make that change. (or maybe I'm just thinking too much and it just slipped away :))

I made the change to use PyCell_GetRef and changed the function to return a new reference (because otherwise it won't be safe in free-threaded build). However, there are a few places in the code where it does not care about the actual value stored, it only cares whether such value exists - so I added a util function to avoid having Py_XDECREFs all around the code.

Comment on lines 165 to 166
PyObject *value = framelocalsproxy_getval(frame->f_frame, co, i);
assert(value != NULL);
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't look safe to me. I don't think we can rely on consistency between the earlier framelocalsproxy_getkeyindex call and the framelocalsproxy_getval here.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Do you mean a very specific case where the content of the cell on that index is set to NULL by PyCell_Set from another thread? I think that's probably an outlier. If it's a normal fast variable, can it happen? Multi-threading can't affect the local fast variables on stack right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I'm just thinking about the cell case.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

The normal Python operation won't set NULL to a cell right? So we should maybe raise some exception here if the value is NULL? That's the only case I can think of.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure about the details here, but in general I think we should avoid this sort of pattern where we perform a "check" separately from a "retrieval" and instead should combine the two. For example, framelocalsproxy_getkeyindex could return the local at the index through an optional output argument.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Yeah that makes sense. I can pass in an pointer to hold the key. I'll work on it.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Okay I made framelocalsproxy_getkeyindex do check & retrieve in a single operation. Tweaked a few other places to make it work.

Copy link
Member

@naschemenascheme left a comment

Choose a reason for hiding this comment

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

LGTM.

@gaogaotiantian
Copy link
MemberAuthor

@colesbury do you have any other suggestions for the current implementation?

Copy link
Contributor

@colesburycolesbury left a comment

Choose a reason for hiding this comment

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

One suggestion below, but otherwise the changes to use new references instead of borrowed references LGTM.

PyObject *extra = frame->f_extra_locals;
if (extra != NULL){
PyObject *value = PyDict_GetItem(extra, key);
value = PyDict_GetItem(extra, key);
Copy link
Contributor

Choose a reason for hiding this comment

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

Use PyDict_GetItemRef if possible?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Done.

@gaogaotiantiangaogaotiantian merged commit 6140b08 into python:mainFeb 27, 2025
41 checks passed
@gaogaotiantiangaogaotiantian deleted the framelocalsproxy-cell branch February 27, 2025 23:12
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@gaogaotiantian@colesbury@nascheme