Skip to content

gh-148037: remove critical section from PyCode_Addr2Line#148103

Open
kumaraditya303 wants to merge 1 commit intopython:mainfrom
kumaraditya303:instrumentation
Open

gh-148037: remove critical section from PyCode_Addr2Line#148103
kumaraditya303 wants to merge 1 commit intopython:mainfrom
kumaraditya303:instrumentation

Conversation

@kumaraditya303
Copy link
Copy Markdown
Contributor

@kumaraditya303 kumaraditya303 commented Apr 4, 2026

TODO: check that no path inside initialize_lines expects lines to be non-NULL.

Copy link
Copy Markdown
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

Two comments below.

An additional thought: in light of things like https://discuss.python.org/t/pure-c-structured-traceback/106872, maybe we should just use atomic operations everywhere for these fields (i.e., _Py_atomic instead of FT_ATOMIC) because PyCode_Addr2Lineis called from _Py_DumpTraceback, which may be called from a signal handler or without holding the GIL.

}
if (co->_co_monitoring && co->_co_monitoring->lines) {
if (FT_ATOMIC_LOAD_PTR_ACQUIRE(co->_co_monitoring) &&
FT_ATOMIC_LOAD_PTR_ACQUIRE(co->_co_monitoring->lines)) {
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 think this sort of reloading of atomic variables is error prone. Structure this like:

_PyCoMonitoringData *data = FT_ATOMIC_LOAD_PTR_ACQUIRE(code->_co_monitoring);
if (data) {
   _PyCoLineInstrumentationData *lines = FT_ATOMIC_LOAD_PTR_ACQUIRE(data->lines);
    if (lines) {
       ...
    }
}

and pass the load line data to _Py_Instrumentation_GetLine.

return -1;
}
initialize_lines(code, bytes_per_entry);
initialize_lines(lines, code, bytes_per_entry);
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.

Minor, and up to you, but when reading this, I think it would be clearer for the atomic store to _co_monitoring->lines to be here instead of inside initialize_lines

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.

2 participants